home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / XLIBP202.ZIP / XLIB2.ASM < prev    next >
Assembly Source File  |  1994-06-18  |  75KB  |  4,782 lines

  1. ; ***************************************************************************
  2. ; **                             XLibPas v2.0                              **
  3. ; **                        for Borland Pascal 7.0                         **
  4. ; **                                  by                                   **
  5. ; **                            Tristan Tarrant                            **
  6. ; ***************************************************************************
  7. ; ** Credits :                                                             **
  8. ; **   Michael Abrash    - Concepts, Algorithms                            **
  9. ; **   Themie Gouthas    - Main code and implementation                    **
  10. ; **   Matthew MacKenzie - Compiled Bitmaps, Circles, etc.                 **
  11. ; **   Tore Bastiansen   - Virtual VSync Handler                           **
  12. ; ***************************************************************************
  13.  
  14.         .MODEL TPASCAL
  15.         .386
  16.         LOCALS
  17.  
  18. include xlib2.inc
  19.         .DATA
  20.  
  21.     OldHandlerSeg      dw  ?
  22.     OldHandlerOffs     dw  ?
  23.     OldHandlerMask     dw  ?
  24.     OldX               dw  ?
  25.     OldY               dw  ?
  26.     OldScrnOffs        dw  ?
  27.  
  28.     level              dw  ?
  29.     gencount           dw  ?
  30.     AdjustedOffs       dw  ?
  31.     p1xt               dw  ?
  32.     p1yt               dw  ?
  33.     p2xt               dw  ?
  34.     p2yt               dw  ?
  35.     p4xt               dw  ?
  36.     p4yt               dw  ?
  37.     p5xt               dw  ?
  38.     p5yt               dw  ?
  39.     c1xt               dw  ?
  40.     c1yt               dw  ?
  41.     c2xt               dw  ?
  42.     c2yt               dw  ?
  43.     xdiff              dw  ?
  44.     ydiff              dw  ?
  45.     moveline           dw  ?
  46.     stepper            dw  ?
  47.     bptemp             dw  ?
  48.     Colort             dw  ?
  49.  
  50.     TicksPerSecond         dw ?
  51.     VsyncIntTicks          label dword
  52.     VsyncIntTicksLo        dw ?
  53.     VsyncIntTicksHi        dw ?
  54.     VsyncPeriod            dw ?       ;Time (in clicks) between each vsync
  55.                                                                         ;1 click = 1.193 microseconds
  56.  
  57.     ClockRate              dw ?             ;Clock rate (in clicks) for timer 0
  58.     ClockCounter           dw ?             ;Counts total clicks modulo 65536
  59.     UserVsyncHandler       label dword      ;Pointer to user routine called
  60.     UserVsyncOffs          dw ?             ;called once each vsync period.
  61.     UserVsyncSeg           dw ?
  62.     InUserHandler          dw ?
  63.                 db 100h dup(?)
  64.     LocalStack             label byte       ;Local stack for user handler
  65.     StackSeg               dw ?
  66.     StackPtr               dw ?
  67.  
  68.  
  69.     ElapsedVrts            dw ?
  70.     VrtsToSkip             dw ?
  71.     MouseVSyncHandler      dd ?
  72.  
  73.  
  74.         .CODE
  75.  
  76. XSetMode PROC FAR Mode : Word, WidthInPixels : Word
  77.  
  78.     mov InGraphics,0
  79.     mov ErrorValue,0
  80.     mov DoubleScanFlag,0
  81.     mov CurrXMode,0
  82.     mov ScrnPhysicalByteWidth,0
  83.     mov ScrnPhysicalPixelWidth,0
  84.     mov ScrnPhysicalHeight,0
  85.     mov SplitScrnOffs,0
  86.     mov SplitScrnScanLine,0
  87.     mov SplitScrnVisibleHeight,0
  88.     mov SplitScrnActive,0
  89.     mov Page0Offs,0
  90.     mov Page1Offs,0
  91.     mov Page2Offs,0
  92.     mov ScrnLogicalByteWidth,0
  93.     mov ScrnLogicalPixelWidth,0
  94.     mov ScrnLogicalHeight,0
  95.     mov MaxScrollX,0
  96.     mov MaxScrollY,0
  97.     mov DoubleBufferActive,0
  98.     mov VisiblePageIdx,0
  99.     mov HiddenPageOffs,0
  100.     mov VisiblePageOffs,0
  101.     mov NonVisualOffs,0
  102.     mov TopClip,0
  103.     mov BottomClip,0
  104.     mov LeftClip,0
  105.     mov RightClip,0
  106.     mov PhysicalStartPixelX,0
  107.     mov PhysicalStartByteX,0
  108.     mov PhysicalStartY,0
  109. IFDEF _DPMI_
  110.     mov    ax,__AHIncr
  111.     mov SelectorInc,ax
  112.     mov ax, __0000H
  113.     mov Seg0000,ax
  114.     mov ax, __A000H
  115. ELSE
  116.     mov ax, 0A000h
  117. ENDIF
  118.     mov ScreenSeg,ax
  119.     cld
  120.     mov   ax,ds
  121.     mov   es,ax
  122.     mov   cx,mode
  123.     cmp   cx,LastXMode
  124.     jle   @@ValidMode
  125.     mov   InGraphics,0
  126.     mov   ax,-1
  127.     jmp @@Done
  128.  
  129. @@ValidMode:
  130.     mov   CurrXMode,cx
  131.     mov   InGraphics,1
  132.     xor   al,al
  133.     cmp   cx,3
  134.     jg    @@SetDoubleScanFlag
  135.     mov   al,1
  136.  
  137. @@SetDoubleScanFlag:
  138.     mov   DoubleScanFlag,al
  139.     push  cx
  140.     mov   ax,13h
  141.     int   10h
  142.     pop   cx
  143.     mov   dx,ScIndex
  144.     mov   ax,0604h
  145.     out   dx,ax
  146.     mov   ax,0100h
  147.     out   dx,ax
  148.     mov   bx,offset ModeTable
  149.     shl   cx,1
  150.     add   bx,cx
  151.     mov   si, word ptr [bx]
  152.     lodsb
  153.     or    al,al
  154.     jz    @@DontSetDot
  155.     mov   dx,MISCOUTPUT
  156.     out   dx,al
  157.  
  158. @@DontSetDot:
  159.     mov   dx,ScIndex
  160.     mov   ax,0300h
  161.     out   dx,ax
  162.     mov   dx,CrtcIndex
  163.     mov   al,11h
  164.     out   dx,al
  165.     inc   dx
  166.     in    al,dx
  167.     and   al,07fh
  168.     out   dx,al
  169.     dec   dx
  170.     cld
  171.     xor   cx,cx
  172.     lodsb
  173.     mov   cl,al
  174.  
  175. @@SetCRTParmsLoop:
  176.     lodsw
  177.  
  178.     out   dx,ax
  179.     loop  @@SetCRTParmsLoop
  180.     mov   dx,ScIndex
  181.     mov   ax,0f02h
  182.     out   dx,ax
  183.     mov   ax,ScreenSeg
  184.     mov   es,ax
  185.     sub   di,di
  186.     sub   ax,ax
  187.     mov   cx,8000h
  188.     rep   stosw
  189.     lodsw
  190.     mov   ScrnPhysicalPixelWidth,ax
  191.     mov   SplitScrnScanLine,ax
  192.     mov   bx,ax
  193.     shr   ax,2
  194.     mov   ScrnPhysicalByteWidth,ax
  195.     lodsw
  196.     mov   ScrnPhysicalHeight,ax
  197.     mov   cx,widthinpixels
  198.     mov   dx,CrtcIndex
  199.     mov   al,CRTCOFFSET
  200.     out   dx,al
  201.     inc   dx
  202.     mov   ax,cx
  203.     cmp   ax,ScrnPhysicalPixelWidth
  204.     jge   @@ValidLogicalWidth
  205.     mov   ax,bx
  206.  
  207. @@ValidLogicalWidth:
  208.     shr   ax,3
  209.     out   dx,al
  210.     shl   ax,1
  211.     mov   bx,ax
  212.     mov   ScrnLogicalByteWidth,ax
  213.     mov   RightClip,ax
  214.     sub   ax,ScrnPhysicalByteWidth
  215.     shl   ax,2
  216.     mov   MaxScrollX,ax
  217.     mov   ax,bx
  218.     shl   ax,2
  219.     mov   ScrnLogicalPixelWidth,ax
  220.     mov   cx,ax
  221.     mov   ax,0ffffh
  222.     sub   dx,dx
  223.     div   bx
  224.     mov   ScrnLogicalHeight,ax
  225.     mov   BottomClip,ax
  226.     sub   ax,ScrnPhysicalHeight
  227.     mov   MaxScrollY,ax
  228.     mov   ax,cx
  229.     mov   ax,ScrnLogicalByteWidth
  230.     mul   ScrnPhysicalHeight
  231.     mov   NonVisualOffs,ax
  232.  
  233. @@Done:
  234.     mov   ax, 0CB00h
  235.     int   10h
  236.     ret
  237.  
  238. XSetMode ENDP
  239.  
  240. XSelectDefaultPlane PROC FAR Plane : Byte
  241.  
  242.     mov  cl,Plane
  243.     and  cl,011b
  244.     mov  ax,0100h + MapMask
  245.     shl  ah,cl
  246.     mov  dx,ScIndex
  247.     out  dx,ax
  248.     mov  ah,cl
  249.     mov  al,ReadMap
  250.     mov  dx,GcIndex
  251.     out  dx,ax
  252.     ret
  253.  
  254. XSelectDefaultPlane ENDP
  255.  
  256. XSetSplitscreen PROC FAR Line : Word
  257.  
  258.     xor  si,si
  259.     cmp  DoubleBufferActive,0
  260.     jne  @@error
  261.     cmp  SplitScrnActive,0
  262.     je   @@NotPreviouslyCalled
  263.  
  264. @@error:
  265.     mov  ErrorValue,ERROR
  266.     ret
  267.  
  268. @@NotPreviouslyCalled:
  269.     mov  dx,InputStatus0
  270.     in   al,dx
  271.     mov  al,ACMODECONTROL+20h
  272.     mov  dx,ACINDEX
  273.     out  dx,al
  274.     inc  dx
  275.     in   al,dx
  276.     or   al,20h
  277.     dec  dx
  278.     out  dx,al
  279.     mov  PhysicalStartByteX,ax
  280.     mov  PhysicalStartPixelX,ax
  281.     mov  PhysicalStartY,ax
  282.     mov  SplitScrnActive,1
  283.     mov  ax,Line
  284.     jns  @@NotNeg
  285.     mov  ax,0
  286.  
  287. @@NotNeg:
  288.     mov  SplitScrnScanLine,ax
  289.     or   DoubleScanFlag,0
  290.     jz   @@NotDoubleScanned
  291.     shl  ax,1
  292.     dec  ax
  293.  
  294. @@NotDoubleScanned:
  295.     mov  bx,ax
  296.     mov  dx,InputStatus0
  297.  
  298. @@WaitNotVsync:
  299.     in   al,dx
  300.     test al,08h
  301.     jnz  @@WaitNotVsync
  302.  
  303. @@WaitVsync:
  304.     in   al,dx
  305.     test al,08h
  306.     jz   @@WaitVsync
  307.     cli
  308.     mov  dx,CrtcIndex
  309.     mov  ah,bl
  310.     mov  al,LINECOMPARE
  311.     out  dx,ax
  312.     mov  ah,bh
  313.     and  ah,1
  314.     shl  ah,4
  315.     mov  al,Overflow
  316.     out  dx,al
  317.     inc  dx
  318.     in   al,dx
  319.     and  al, not 10h
  320.     or   al,ah
  321.     out  dx,al
  322.     dec  dx
  323.     mov  ah,bh
  324.     and  ah,2
  325.     ror  ah,3
  326.     mov  al,MAXSCANLINE
  327.     out  dx,al
  328.     inc  dx
  329.     in   al,dx
  330.     and  al, not 40h
  331.     or   al,ah
  332.     out  dx,al
  333.     sti
  334.     mov  ax,ScrnPhysicalHeight
  335.     sub  ax,SplitScrnScanLine
  336.     mov  SplitScrnVisibleHeight,ax
  337.     mov  bx,ScrnLogicalByteWidth
  338.     mul  bx
  339.     mov  Page0Offs,ax
  340.     mov  Page1Offs,ax
  341.     mov  Page2Offs,ax
  342.     mov  cx,0ffffh
  343.     sub  cx,ax
  344.     xchg cx,ax
  345.     sub  dx,dx
  346.     div  bx
  347.     mov  ScrnLogicalHeight,ax
  348.     cmp  ax,BottomClip
  349.     jle  @@BottomClipOK
  350.     mov  BottomClip,ax
  351.  
  352. @@BottomClipOK:
  353.     sub  ax,SplitScrnScanLine
  354.     mov  MaxScrollY,ax
  355.     xchg cx,ax
  356.     mov  bh,al
  357.     mov  ch,ah
  358.     mov  bl,ADDRLOW
  359.     mov  cl,ADDRHIGH
  360.     and  si,0003h
  361.     mov  ah,byte ptr PelPanMask[si]
  362.     mov  al,PELPANNING+20h
  363.     mov  si,ax
  364.  
  365. ; VSync Handler stuff
  366.  
  367.     cmp  VSyncHandlerActive, TRUE
  368.     jne   @@NoVsyncHandler
  369.  
  370. @@WaitLast:
  371.     cmp   StartAddressFlag,0
  372.     jne   @@WaitLast
  373.     cli
  374.     mov  WaitingStartLow,bx
  375.     mov  WaitingStartHigh,cx
  376.     mov  WaitingPelPan,si
  377.     mov  StartAddressFlag,1
  378.     sti
  379.     jmp  short @@Return
  380.  
  381. ; End VSync Handler stuff
  382.  
  383. @@NoVsyncHandler:
  384.     mov  dx,InputStatus0
  385.  
  386. @@WaitDE:
  387.     in   al,dx
  388.     test al,01h
  389.     jnz  @@WaitDE
  390.     mov  dx,CrtcIndex
  391.     mov  ax,bx
  392.     cli
  393.     out  dx,ax
  394.     mov  ax,cx
  395.     out  dx,ax
  396.     sti
  397.     mov  dx,InputStatus0
  398.  
  399. @@WaitVS:
  400.     in   al,dx
  401.     test al,08h
  402.     jz @@WaitVS
  403.  
  404.     mov  dx,ACINDEX
  405.     mov  ax,si
  406.     cli
  407.     out  dx,al
  408.     mov  al,ah
  409.     out  dx,al
  410.     sti
  411.  
  412. @@Return:
  413.     mov  ErrorValue,OK
  414.     ret
  415.  
  416. XSetSplitScreen ENDP
  417.  
  418. XSetStartAddr PROC FAR X : Word, Y : Word
  419.  
  420.     mov  si,x
  421.     mov  ax,ScrnLogicalByteWidth
  422.     mov  cx,y
  423.     mul  cx
  424.     cmp  DoubleBufferActive,1
  425.     je   @@PageResolution
  426.     cmp  TripleBufferActive,1
  427.     je   @@PageResolution
  428.  
  429. @@PageFlipEntry1:
  430.     add  ax,Page0Offs
  431.     jmp  @@AddColumn
  432.  
  433. @@PageFlipEntry2:
  434.     mov  PhysicalStartPixelX,si
  435.     mov  PhysicalStartY,cx
  436.  
  437. @@PageResolution:
  438.     add  ax,VisiblePageOffs
  439.  
  440. @@AddColumn:
  441.     mov  cx,si
  442.     shr  cx,2
  443.     mov  PhysicalStartByteX,cx
  444.     add  ax,cx
  445.     mov  bh,al
  446.     mov  ch,ah
  447.  
  448. @@StartAddrEntry:
  449.     mov  bl,ADDRLOW
  450.     mov  cl,ADDRHIGH
  451.     and  si,0003h
  452.     mov  ah,byte ptr PelPanMask[si]
  453.     mov  al,PELPANNING+20h
  454.     mov  si,ax
  455.  
  456. ; VSync Handler stuff
  457.  
  458.     cmp  VSyncHandlerActive, TRUE
  459.     jne   @@NoVsyncHandler
  460.  
  461. @@WaitLast:
  462.     cmp   StartAddressFlag,0
  463.     jne   @@WaitLast
  464.     cli
  465.     mov  WaitingStartLow,bx
  466.     mov  WaitingStartHigh,cx
  467.     mov  WaitingPelPan,si
  468.     mov  StartAddressFlag,1
  469.     sti
  470.     jmp  short @@Return
  471.  
  472. ; End VSync Handler stuff
  473.  
  474. @@NoVsyncHandler:
  475.     mov  dx,InputStatus0
  476.  
  477. @@WaitDE:
  478.     in   al,dx
  479.     test al,01h
  480.     jnz  @@WaitDE
  481.     mov  dx,CrtcIndex
  482.     mov  ax,bx
  483.     cli
  484.     out  dx,ax
  485.     mov  ax,cx
  486.     out  dx,ax
  487.     sti
  488.     mov  dx,InputStatus0
  489.  
  490. @@WaitVS:
  491.     in   al,dx
  492.     test al,08h
  493.     jz @@WaitVS
  494.  
  495.     mov  dx,ACINDEX
  496.     mov  ax,si
  497.     cli
  498.     out  dx,al
  499.     mov  al,ah
  500.     out  dx,al
  501.     sti
  502.  
  503. @@Return:
  504.     mov  ErrorValue,OK
  505.     ret
  506.  
  507. XSetStartAddr ENDP
  508.  
  509. XHideSplitscreen PROC FAR
  510.  
  511.     cmp  SplitScrnActive,1
  512.     je   @@SplitScreenEnabled
  513.  
  514. @@error:
  515.     mov  ErrorValue,ERROR
  516.     jmp  @@Done
  517.  
  518. @@SplitScreenEnabled:
  519.     cmp  CurrXMode,4
  520.     jg   @@error
  521.     mov  bx,ScrnPhysicalHeight
  522.     mov  ax,ScrnLogicalHeight
  523.     sub  ax,bx
  524.     mov  MaxScrollY,ax
  525.     xor  ax,ax
  526.     mov  SplitScrnVisibleHeight,ax
  527.     or   DoubleScanFlag,0
  528.     jz   @@NotDoubleScanned
  529.     shl  bx,1
  530.     dec  bx
  531.  
  532. @@NotDoubleScanned:
  533.     cmp  VSyncHandlerActive,1
  534.     je   @@VSyncHandling
  535.     mov  dx,InputStatus0
  536.  
  537. @@WaitNotVsync:
  538.     in   al,dx
  539.     test al,08h
  540.     jnz  @@WaitNotVsync
  541.  
  542. @@WaitVsync:
  543.     in   al,dx
  544.     test al,08h
  545.     jz   @@WaitVsync
  546.     jmp  short @@SetRegs
  547. @@VSyncHandling:
  548.     cmp  StartAddressFlag,0
  549.     jne  @@VSyncHandling
  550.     mov  StartAddressFlag,1
  551. @@SetRegs:
  552.     cli
  553.     mov  dx,CrtcIndex
  554.     mov  ah,bl
  555.     mov  al,LINECOMPARE
  556.     out  dx,ax
  557.     mov  ah,bh
  558.     and  ah,1
  559.     shl  ah,4
  560.     mov  al,Overflow
  561.     out  dx,al
  562.     inc  dx
  563.     in   al,dx
  564.     and  al, not 10h
  565.     or   al,ah
  566.     out  dx,al
  567.     dec  dx
  568.     mov  ah,bh
  569.     and  ah,2
  570.     ror  ah,3
  571.     mov  al,MAXSCANLINE
  572.     out  dx,al
  573.     inc  dx
  574.     in   al,dx
  575.     and  al, not 40h
  576.     or   al,ah
  577.     out  dx,al
  578.     sti
  579.  
  580. @@done:
  581.     mov  ErrorValue,OK
  582.     ret
  583.  
  584. XHideSplitScreen ENDP
  585.  
  586. XShowSplitscreen PROC FAR
  587.  
  588.     cmp  SplitScrnActive,1
  589.     je   @@SplitScreenEnabled
  590.  
  591. @@error:
  592.     mov  ErrorValue,ERROR
  593.     jmp  @@Done
  594.  
  595. @@SplitScreenEnabled:
  596.     cmp  CurrXMode,4
  597.     jg   @@error
  598.     mov  bx,SplitScrnScanLine
  599.     mov  ax,ScrnLogicalHeight
  600.     sub  ax,bx
  601.     mov  MaxScrollY,ax
  602.     mov  ax,ScrnPhysicalHeight
  603.     sub  ax,bx
  604.     mov  SplitScrnVisibleHeight,ax
  605.     or   DoubleScanFlag,0
  606.     jz   @@NotDoubleScanned
  607.     shl  bx,1
  608.     dec  bx
  609.  
  610. @@NotDoubleScanned:
  611.     cmp  VsyncHandlerActive,1
  612.     je   @@VSyncHandling
  613.     mov  dx,InputStatus0
  614.  
  615. @@WaitNotVsync:
  616.     in   al,dx
  617.     test al,08h
  618.     jnz  @@WaitNotVsync
  619.  
  620. @@WaitVsync:
  621.     in   al,dx
  622.     test al,08h
  623.     jz   @@WaitVsync
  624.     jmp short @@SetRegs
  625. @@VSyncHandling:
  626.     cmp  StartAddressFlag,0
  627.     jne  @@VSyncHandling
  628.     mov  StartAddressFlag,1
  629. @@SetRegs:
  630.     cli
  631.     mov  dx,CrtcIndex
  632.     mov  ah,bl
  633.     mov  al,LINECOMPARE
  634.     out  dx,ax
  635.     mov  ah,bh
  636.     and  ah,1
  637.     shl  ah,4
  638.     mov  al,Overflow
  639.     out  dx,al
  640.     inc  dx
  641.     in   al,dx
  642.     and  al, not 10h
  643.     or   al,ah
  644.     out  dx,al
  645.     dec  dx
  646.     mov  ah,bh
  647.     and  ah,2
  648.     ror  ah,3
  649.     mov  al,MAXSCANLINE
  650.     out  dx,al
  651.     inc  dx
  652.     in   al,dx
  653.     and  al, not 40h
  654.     or   al,ah
  655.     out  dx,al
  656.     sti
  657.  
  658. @@Done:
  659.     mov  ErrorValue,0
  660.     ret
  661.  
  662. XShowSplitScreen ENDP
  663.  
  664. XAdjustSplitScreen PROC FAR Line : Word
  665.  
  666.     cmp  SplitScrnActive,1
  667.     je   @@SplitScreenEnabled
  668.  
  669. @@error:
  670.     mov  ErrorValue,ERROR
  671.     jmp  @@Done
  672.  
  673. @@SplitScreenEnabled:
  674.     cmp  CurrXMode,4
  675.     jg   @@error
  676.     mov  bx,Line
  677.     cmp  bx,SplitScrnScanLine
  678.     js   @@Done
  679.  
  680. @@ValidScanLine:
  681.     mov  ax,ScrnLogicalHeight
  682.     sub  ax,bx
  683.     mov  MaxScrollY,ax
  684.     mov  ax,ScrnPhysicalHeight
  685.     sub  ax,bx
  686.     mov  SplitScrnVisibleHeight,ax
  687.     or   DoubleScanFlag,0
  688.     jz   @@NotDoubleScanned
  689.     shl  bx,1
  690.     dec  bx
  691.  
  692. @@NotDoubleScanned:
  693.     cmp  VsyncHandlerActive,1
  694.     je   @@VsyncHandling
  695.  
  696.     mov  dx,InputStatus0
  697.  
  698. @@WaitNotVsync:
  699.     in   al,dx
  700.     test al,08h
  701.     jnz  @@WaitNotVsync
  702.  
  703. @@WaitVsync:
  704.     in   al,dx
  705.     test al,08h
  706.     jz   @@WaitVsync
  707.     jmp  short @@SetRegs
  708.  
  709. @@VsyncHandling:
  710.     cmp  StartAddressFlag,0
  711.     jne  @@VSyncHandling
  712.     mov  StartAddressFlag,2
  713. @@SetRegs:
  714.     cli
  715.     mov  dx,CrtcIndex
  716.     mov  ah,bl
  717.     mov  al,LINECOMPARE
  718.     out  dx,ax
  719.     mov  ah,bh
  720.     and  ah,1
  721.     shl  ah,4
  722.     mov  al,Overflow
  723.     out  dx,al
  724.     inc  dx
  725.     in   al,dx
  726.     and  al, not 10h
  727.     or   al,ah
  728.     out  dx,al
  729.     dec  dx
  730.     mov  ah,bh
  731.     and  ah,2
  732.     ror  ah,3
  733.     mov  al,MAXSCANLINE
  734.     out  dx,al
  735.     inc  dx
  736.     in   al,dx
  737.     and  al, not 40h
  738.     or   al,ah
  739.     out  dx,al
  740.     sti
  741.  
  742. @@Done:
  743.     mov  ErrorValue,OK
  744.     ret
  745.  
  746. XAdjustSplitScreen ENDP
  747.  
  748. XSetDoubleBuffer PROC FAR PageHeight : Word
  749.  
  750.     cmp   DoubleBufferActive,0
  751.     je    @@OkToContinue
  752. @error:
  753.     mov   ErrorValue,ERROR
  754.     ret
  755.  
  756. @@OkToContinue:
  757.     mov  VisiblePageIdx,0
  758.     mov  ax,ScrnLogicalHeight
  759.     shr  ax,1
  760.     mov  bx,PageHeight
  761.     cmp  ax,bx
  762.     js   @@InvalidHeight
  763.     mov  ax,bx
  764.  
  765. @@InvalidHeight:
  766.     mov   ScrnLogicalHeight,ax
  767.     cmp   ax,BottomClip
  768.     jle   @@BottomClipOK
  769.     mov   BottomClip,ax
  770. @@BottomClipOK:
  771.     mov   dx,ax
  772.     mul   ScrnLogicalByteWidth
  773.     mov   cx,ax
  774.     mov   bx,Page0Offs
  775.     mov   VisiblePageOffs,bx
  776.     add   ax,bx
  777.     mov   Page1Offs,ax
  778.     mov   HiddenPageOffs,ax
  779.     add   ax,cx
  780.     mov   NonVisualOffs,ax
  781.     mov   DoubleBufferActive,1
  782.     mov   ax,dx
  783.     sub   ax,ScrnPhysicalHeight
  784.     add   ax,SplitScrnVisibleHeight
  785.     mov   MaxScrollY,ax
  786.     mov   ax,dx
  787.     mov   ErrorValue,OK
  788.     ret
  789.  
  790. XSetDoubleBuffer ENDP
  791.  
  792. XSetTripleBuffer PROC FAR PageHeight : word
  793.  
  794.     cmp   DoubleBufferActive,0
  795.     jne   @@Error
  796.     cmp   TripleBufferActive,0
  797.     je    @@OkToContinue
  798.  
  799. @@Error:
  800.     mov   ErrorValue,ERROR
  801.     jmp @@Done
  802.  
  803. @@OkToContinue:
  804.     mov   VisiblePageIdx,0
  805.     mov   ax,ScrnLogicalHeight
  806.     mov   bx,3
  807.     xor   dx,dx
  808.     idiv  bx
  809.     mov   bx,PageHeight
  810.     cmp   ax,bx
  811.     js    @@InvalidHeight
  812.     mov   ax,bx
  813.  
  814. @@InvalidHeight:
  815.     mov   ScrnLogicalHeight,ax
  816.     cmp   ax,BottomClip
  817.     jle   @@BottomClipOK
  818.     mov   BottomClip,ax
  819.  
  820. @@BottomClipOK:
  821.     push  ax
  822.     mul   ScrnLogicalByteWidth
  823.     mov   cx,ax
  824.     mov   bx,Page0Offs
  825.     mov   VisiblePageOffs,bx
  826.     add   ax,bx
  827.     mov   Page1Offs,ax
  828.     mov   HiddenPageOffs,ax
  829.     add   ax,cx
  830.     mov   Page2Offs,ax
  831.     mov   WaitingPageOffs,ax
  832.     add   ax,cx
  833.     mov   NonVisualOffs,ax
  834.     mov   TripleBufferActive,1
  835.     pop   ax
  836.     sub   ax,ScrnPhysicalHeight
  837.     add   ax,SplitScrnVisibleHeight
  838.     mov   MaxScrollY,ax
  839.     mov   ax,dx
  840.     mov   ErrorValue,OK
  841.  
  842. @@Done:
  843.     ret
  844.  
  845. XSetTripleBuffer ENDP
  846.  
  847. XPageFlip PROC FAR X : Word, Y : Word
  848.  
  849.     mov  si,x
  850.     mov  ax,ScrnLogicalByteWidth
  851.     mov  cx,y
  852.     mul  cx
  853.     cmp  DoubleBufferActive,1
  854.     je   @@DoubleBuffer
  855.     cmp  TripleBufferActive,1
  856.     jne  @@PageFlipEntry1
  857.     mov  bx,HiddenPageOffs
  858.     xchg bx,VisiblePageOffs
  859.     xchg bx,WaitingPageOffs
  860.     mov  HiddenPageOffs,bx
  861.     mov  bx, VisiblePageIdx
  862.     inc  bx
  863.     cmp  bx,3
  864.     jne  @@IdxOk
  865.     xor  bx,bx
  866. @@IdxOk:
  867.     mov  VisiblePageIdx,bx
  868.     jmp  @@PageFlipEntry2
  869.  
  870. @@DoubleBuffer:
  871.     mov  bx,HiddenPageOffs
  872.     xchg bx,VisiblePageOffs
  873.     xchg HiddenPageOffs,bx
  874.     xor  VisiblePageIdx,01h
  875.     jmp  @@PageFlipEntry2
  876.  
  877. @@PageFlipEntry1:
  878.     add  ax,Page0Offs
  879.     jmp  @@AddColumn
  880.  
  881. @@PageFlipEntry2:
  882.     mov  PhysicalStartPixelX,si
  883.     mov  PhysicalStartY,cx
  884.  
  885. @@PageResolution:
  886.     add  ax,VisiblePageOffs
  887.  
  888. @@AddColumn:
  889.     mov  cx,si
  890.     shr  cx,2
  891.     mov  PhysicalStartByteX,cx
  892.     add  ax,cx
  893.     mov  bh,al
  894.     mov  ch,ah
  895.  
  896. @@StartAddrEntry:
  897.     mov  bl,ADDRLOW
  898.     mov  cl,ADDRHIGH
  899.     and  si,0003h
  900.     mov  ah,byte ptr PelPanMask[si]
  901.     mov  al,PELPANNING+20h
  902.     mov  si,ax
  903. ; VSync Handler stuff
  904.  
  905.     cmp  VSyncHandlerActive, TRUE
  906.     jne   @@NoVsyncHandler
  907.  
  908. @@WaitLast:
  909.     cmp   StartAddressFlag,0
  910.     jne   @@WaitLast
  911.     cli
  912.     mov  WaitingStartLow,bx
  913.     mov  WaitingStartHigh,cx
  914.     mov  WaitingPelPan,si
  915.     mov  StartAddressFlag,1
  916.     sti
  917.     jmp  short @@Return
  918.  
  919. ; End VSync Handler stuff
  920.  
  921. @@NoVsyncHandler:
  922.     mov  dx,InputStatus0
  923.  
  924. @@WaitDE:
  925.     in   al,dx
  926.     test al,01h
  927.     jnz  @@WaitDE
  928.     mov  dx,CrtcIndex
  929.     mov  ax,bx
  930.     cli
  931.     out  dx,ax
  932.     mov  ax,cx
  933.     out  dx,ax
  934.     sti
  935.     mov  dx,InputStatus0
  936.  
  937. @@WaitVS:
  938.     in   al,dx
  939.     test al,08h
  940.     jz @@WaitVS
  941.  
  942.     mov  dx,ACINDEX
  943.     mov  ax,si
  944.     cli
  945.     out  dx,al
  946.     mov  al,ah
  947.     out  dx,al
  948.     sti
  949.  
  950. @@Return:
  951.     mov  ErrorValue,OK
  952.     ret
  953.  
  954. XPageFlip ENDP
  955.  
  956. XSetClipRect PROC FAR Left : Word, Top : Word, Right : Word, Bottom : Word
  957.  
  958.     mov   ax,left
  959.     mov   bx,right
  960.     cmp   bx,ax
  961.     jns   @@CorrectXOrder
  962.     xchg  bx,ax
  963. @@CorrectXOrder:
  964.     mov   LeftClip,ax
  965.     mov   RightClip,bx
  966.     mov   ax,top
  967.     mov   bx,bottom
  968.     cmp   bx,ax
  969.     jns   @@CorrectYOrder
  970.     xchg  bx,ax
  971. @@CorrectYOrder:
  972.     mov   TopClip,ax
  973.     mov   BottomClip,bx
  974.     ret
  975.  
  976. XSetClipRect ENDP
  977.  
  978. XTextMode PROC FAR
  979.  
  980.     mov   ax,03h
  981.     int   10h
  982.     ret
  983.  
  984. XTextMode ENDP
  985.  
  986. XWaitVSync PROC FAR
  987.  
  988.     mov   dx,InputStatus0
  989. @@WaitNotVsync:
  990.     in    al,dx
  991.     test  al,08h
  992.     jnz   @@WaitNotVsync
  993.  
  994. @@WaitVsync:
  995.     in    al,dx
  996.     test  al,08h
  997.     jz    @@WaitVsync
  998.     ret
  999.  
  1000. XWaitVSync ENDP
  1001.  
  1002. XLine PROC FAR x1 : Word, y1 : Word, x2 : Word, y2 : Word, Color : Word,\
  1003.                              PgOffs : Word
  1004. LOCAL vertincr : Word, incr1 : Word, incr2 : Word, routine : Word
  1005.  
  1006.     mov ax,ScreenSeg
  1007.     mov es,ax
  1008.  
  1009.     mov dx,ScIndex
  1010.     mov si,ScrnLogicalByteWidth
  1011.     mov cx,x2
  1012.     sub cx,x1
  1013.     jz  @@VertLine
  1014.     jns @@L01
  1015.  
  1016.     neg cx
  1017.  
  1018.     mov bx,x2
  1019.     xchg  bx,x1
  1020.     mov x2,bx
  1021.  
  1022.     mov bx,y2
  1023.     xchg  bx,y1
  1024.     mov y2,bx
  1025.  
  1026. @@L01:
  1027.     mov bx,y2
  1028.     sub bx,y1
  1029.     jnz @@skip
  1030.     jmp @@HorizLine
  1031.  
  1032. @@skip:
  1033.     jns @@L03
  1034.     neg bx
  1035.     neg si
  1036.  
  1037. @@L03:
  1038.     mov vertincr,si
  1039.     mov routine,offset @@LoSlopeLine
  1040.     cmp bx,cx
  1041.     jle @@L04
  1042.     mov routine,offset @@HiSlopeLine
  1043.     xchg  bx,cx
  1044.  
  1045.  
  1046. @@L04:
  1047.     shl bx,1
  1048.     mov incr1,bx
  1049.     sub bx,cx
  1050.     mov si,bx
  1051.     sub bx,cx
  1052.     mov incr2,bx
  1053.  
  1054.     push  cx
  1055.     mov ax,y1
  1056.     mov bx,x1
  1057.     mov cl,bl
  1058.     push dx
  1059.     mov dx,ScrnLogicalByteWidth
  1060.     mul dx
  1061.     pop dx
  1062.     shr bx,2
  1063.     add bx,ax
  1064.     add bx,PgOffs
  1065.     and cl,3
  1066.     mov di,bx
  1067.     mov al,1
  1068.     shl al,cl
  1069.     mov ah,al
  1070.     shl al,4
  1071.     add ah,al
  1072.     mov bl,ah
  1073.     pop cx
  1074.     inc cx
  1075.     jmp routine
  1076.  
  1077. @@VertLine:
  1078.     mov ax,y1
  1079.     mov bx,y2
  1080.     mov cx,bx
  1081.     sub cx,ax
  1082.     jge @@L31
  1083.     neg cx
  1084.     mov ax,bx
  1085.  
  1086. @@L31:
  1087.     inc cx
  1088.     mov bx,x1
  1089.     push  cx
  1090.     mov cl,bl
  1091.     push dx
  1092.     mov dx,ScrnLogicalByteWidth
  1093.     mul dx
  1094.     pop dx
  1095.     shr bx,2
  1096.     add bx,ax
  1097.     add bx,PgOffs
  1098.     and cl,3
  1099.  
  1100.     mov ah,1
  1101.     shl ah,cl
  1102.     mov al,MapMask
  1103.     out dx,ax
  1104.     pop cx
  1105.     mov ax, word ptr [Color]
  1106.  
  1107. @@L32:
  1108.     mov es:[bx],al
  1109.     add bx,si
  1110.     loop  @@L32
  1111.     jmp @@Lexit
  1112.  
  1113. @@HorizLine:
  1114.     push  ds
  1115.  
  1116.     mov ax,y1
  1117.     mov bx,x1
  1118.     mov cl,bl
  1119.     push dx
  1120.     mov dx,ScrnLogicalByteWidth
  1121.     mul dx
  1122.     pop dx
  1123.     shr bx,2
  1124.     add bx,ax
  1125.     add bx,PgOffs
  1126.     and cl,3
  1127.  
  1128.     mov di,bx
  1129.     mov dl,00fh
  1130.     shl dl,cl
  1131.  
  1132.     mov cx,x2
  1133.     and cl,3
  1134.     mov dh,00eh
  1135.     shl dh,cl
  1136.     not dh
  1137.  
  1138.     mov ax,x2
  1139.     mov bx,x1
  1140.  
  1141.     shr ax,2
  1142.     shr bx,2
  1143.     mov cx,ax
  1144.     sub cx,bx
  1145.  
  1146.     mov ax,dx
  1147.     mov dx,SCINDEX
  1148.     mov bx, [Color]
  1149.     or  cx,cx
  1150.     jnz @@L42
  1151.     and ah,al
  1152.     jmp @@L44
  1153.  
  1154. @@L42:
  1155.     push ax
  1156.     mov ah,al
  1157.     mov al,MAPMASK
  1158.     out dx,ax
  1159.     mov al,bl
  1160.     stosb
  1161.     dec cx
  1162.  
  1163. @@L43:
  1164.     mov ah,0Fh
  1165.     mov al,MAPMASK
  1166.     out dx,ax
  1167.     mov al,bl
  1168.     rep stosb
  1169.     pop     ax
  1170.  
  1171. @@L44:
  1172.     mov al,MAPMASK
  1173.     out dx, ax
  1174.     mov     byte ptr es:[di],bl
  1175.     pop ds
  1176.     jmp @@Lexit
  1177.  
  1178.  
  1179. @@LoSlopeLine:
  1180.     mov al,MAPMASK
  1181.     mov bh,byte ptr [Color]
  1182. @@L10:
  1183.     mov ah,bl
  1184.  
  1185. @@L11:
  1186.     or  ah,bl
  1187.     rol bl,1
  1188.     jc  @@L14
  1189.  
  1190.     or  si,si
  1191.     jns @@L12
  1192.     add si,incr1
  1193.     loop  @@L11
  1194.  
  1195.     out dx,ax
  1196.     mov es:[di],bh
  1197.     jmp @@Lexit
  1198.  
  1199. @@L12:
  1200.     add si,incr2
  1201.     out dx,ax
  1202.     mov es:[di],bh
  1203.     add di,vertincr
  1204.     loop  @@L10
  1205.     jmp @@Lexit
  1206.  
  1207. @@L14:
  1208.     out dx,ax
  1209.     mov es:[di],bh
  1210.     inc di
  1211.     or  si,si
  1212.     jns @@L15
  1213.     add si,incr1
  1214.     loop  @@L10
  1215.     jmp @@Lexit
  1216.  
  1217. @@L15:
  1218.     add si,incr2
  1219.     add di,vertincr
  1220.     loop  @@L10
  1221.     jmp @@Lexit
  1222.  
  1223. @@HiSlopeLine:
  1224.     mov bx,vertincr
  1225.     mov al,MAPMASK
  1226. @@L21:
  1227.     out dx,ax
  1228.     push  ax
  1229.     mov ax,Color
  1230.     mov es:[di],al
  1231.     pop ax
  1232.     add di,bx
  1233.  
  1234. @@L22:
  1235.     or  si,si
  1236.     jns @@L23
  1237.  
  1238.     add si,incr1
  1239.     loop  @@L21
  1240.     jmp @@Lexit
  1241.  
  1242. @@L23:
  1243.     add si,incr2
  1244.     rol ah,1
  1245.     adc di,0
  1246. @@lx21: loop  @@L21
  1247.  
  1248.  
  1249. @@Lexit:
  1250.     ret
  1251.  
  1252. XLine ENDP
  1253.  
  1254. XPutPix PROC FAR X : Word, Y : Word, PgOfs : Word, Color : Word
  1255.  
  1256.     mov  ax,ScrnLogicalByteWidth
  1257.     mul  Y
  1258.     mov  bx,X
  1259.     shr  bx,2
  1260.     add  bx,ax
  1261.     add  bx,PgOfs
  1262.     mov  ax,SCREENSEG
  1263.     mov  es,ax
  1264.  
  1265.     mov  cl,byte ptr X
  1266.     and  cl,011b
  1267.     mov  ax,0100h + MAPMASK
  1268.     shl  ah,cl
  1269.     mov  dx,SCINDEX
  1270.     out  dx,ax
  1271.     mov  al,byte ptr Color
  1272.     mov  es:[bx],al
  1273.     ret
  1274.  
  1275. XPutPix ENDP
  1276.  
  1277. XGetPix PROC FAR x : Word, y : Word, PageBase : Word
  1278.  
  1279.     mov  ax,ScrnLogicalByteWidth
  1280.     mul  Y
  1281.     mov  bx,X
  1282.     shr  bx,1
  1283.     shr  bx,1
  1284.     add  bx,ax
  1285.     add  bx,PageBase
  1286.     mov  ax,SCREENSEG
  1287.     mov  es,ax
  1288.     mov  ah,byte ptr X
  1289.     and  ah,011b
  1290.     mov  al,READMAP
  1291.     mov  dx,GCINDEX
  1292.     out  dx,ax
  1293.     mov  al,es:[bx]
  1294.     sub  ah,ah
  1295.     ret
  1296.  
  1297. XGetPix ENDP
  1298.  
  1299. XRectFill PROC FAR StartX : Word, StartY : Word, EndX : Word, EndY : Word,\
  1300.                         PageBase : Word, Color : Word
  1301.  
  1302.     push bp
  1303.     cld
  1304.     mov  ax,ScrnLogicalByteWidth
  1305.     mul  StartY
  1306.     mov  di,StartX
  1307.     shr  di,1
  1308.     shr  di,1
  1309.     add  di,ax
  1310.     add  di,PageBase
  1311.     mov  ax,SCREENSEG
  1312.     mov  es,ax
  1313.     mov  dx,SCINDEX
  1314.     mov  al,MAPMASK
  1315.     out  dx,al
  1316.     inc  dx
  1317.     mov  si,StartX
  1318.     and  si,0003h
  1319.     mov  bh,byte ptr LeftClipPlaneMask[si]
  1320.     mov  si,EndX
  1321.     and  si,0003h
  1322.     mov  bl,byte ptr RightClipPlaneMask[si]
  1323.     mov  cx,EndX
  1324.     mov  si,StartX
  1325.     cmp  cx,si
  1326.     jle  @@FillDone
  1327.     dec  cx
  1328.     and  si,not 011b
  1329.     sub  cx,si
  1330.     shr  cx,1
  1331.     shr  cx,1
  1332.     jnz  @@MasksSet
  1333.     and  bh,bl
  1334.  
  1335. @@MasksSet:
  1336.     mov  si,EndY
  1337.     sub  si,StartY
  1338.     jle  @@FillDone
  1339.     mov  ah,byte ptr Color
  1340.     mov  bp,ScrnLogicalByteWidth
  1341.     sub  bp,cx
  1342.     dec  bp
  1343. @@FillRowsLoop:
  1344.     push cx
  1345.     mov  al,bh
  1346.     out  dx,al
  1347.     mov  al,ah
  1348.     stosb
  1349.     dec  cx
  1350.     js   @@FillLoopBottom
  1351.     jz   @@DoRightEdge
  1352.     mov  al,00fh
  1353.     out  dx,al
  1354.     mov  al,ah
  1355.     rep  stosb
  1356. @@DoRightEdge:
  1357.     mov  al,bl
  1358.     out  dx,al
  1359.     mov  al,ah
  1360.     stosb
  1361. @@FillLoopBottom:
  1362.     add  di,bp
  1363.     pop  cx
  1364.     dec  si
  1365.     jnz  @@FillRowsLoop
  1366. @@FillDone:
  1367.     pop bp
  1368.     ret
  1369.  
  1370. XRectFill ENDP
  1371.  
  1372.  
  1373.  
  1374. XRectPattern PROC FAR StartX : Word, StartY : Word, EndX : Word, EndY : Word,\
  1375.                                             PageBase : Word, Pattern : DWord
  1376. LOCAL NextScanOffset : Word, RectAddrWidth : Word, Height : word
  1377.  
  1378.     push ds
  1379.     cld
  1380.     mov  ax,SCREENSEG
  1381.     mov  es,ax
  1382.     lds  si,Pattern
  1383.     mov  di,PATTERNBUFFER
  1384.     mov  dx,SCINDEX
  1385.     mov  al,MAPMASK
  1386.     out  dx,al
  1387.     inc  dx
  1388.     mov  cx,4
  1389. @@DownloadPatternLoop:
  1390.     mov  al,1
  1391.     out  dx,al
  1392.     movsb
  1393.     dec  di
  1394.     mov  al,2
  1395.     out  dx,al
  1396.     movsb
  1397.     dec  di
  1398.     mov  al,4
  1399.     out  dx,al
  1400.     movsb
  1401.     dec  di
  1402.     mov  al,8
  1403.     out  dx,al
  1404.     movsb
  1405.     loop @@DownloadPatternLoop
  1406.     pop  ds
  1407.  
  1408.     mov  dx,GCINDEX
  1409.     mov  ax,00000h+BITMASK
  1410.     out  dx,ax
  1411.  
  1412.     mov  ax,StartY
  1413.     mov  si,ax
  1414.     and  si,011b
  1415.     add  si,PATTERNBUFFER
  1416.  
  1417.     mov  dx,ScrnLogicalByteWidth
  1418.     mul  dx
  1419.     mov  di,StartX
  1420.     mov  bx,di
  1421.     shr  di,1
  1422.     shr  di,1
  1423.     add  di,ax
  1424.     add  di,PageBase
  1425.  
  1426.     and  bx,0003h
  1427.     mov  ah,byte ptr LeftClipPlaneMask[bx]
  1428.     mov  bx,EndX
  1429.     and  bx,0003h
  1430.     mov  al,byte ptr RightClipPlaneMask[bx]
  1431.     mov  bx,ax
  1432.  
  1433.     mov  cx,EndX
  1434.     mov  ax,StartX
  1435.     cmp  cx,ax
  1436.     jle  @@FillDone
  1437.     dec  cx
  1438.     and  ax,not 011b
  1439.     sub  cx,ax
  1440.     shr  cx,1
  1441.     shr  cx,1
  1442.     jnz  @@MasksSet
  1443.     and  bh,bl
  1444.  
  1445. @@MasksSet:
  1446.     mov  ax,EndY
  1447.     sub  ax,StartY
  1448.     jle  @@FillDone
  1449.     mov  Height,ax
  1450.     mov  ax,ScrnLogicalByteWidth
  1451.     sub  ax,cx
  1452.     dec  ax
  1453.     mov  NextScanOffset,ax
  1454.     mov  RectAddrWidth,cx
  1455.     mov  dx,SCINDEX+1
  1456.  
  1457. @@FillRowsLoop:
  1458.     mov  cx,RectAddrWidth
  1459.     mov  al,es:[si]
  1460.     inc  si
  1461.     jnz  @@NoWrap
  1462.     sub  si,4
  1463. @@NoWrap:
  1464.     mov  al,bh
  1465.     out  dx,al
  1466.     stosb
  1467.  
  1468.     dec  cx
  1469.     js   @@FillLoopBottom
  1470.     jz   @@DoRightEdge
  1471.     mov  al,00fh
  1472.     out  dx,al
  1473.     rep  stosb
  1474.  
  1475. @@DoRightEdge:
  1476.     mov  al,bl
  1477.     out  dx,al
  1478.     stosb
  1479.  
  1480. @@FillLoopBottom:
  1481.     add  di,NextScanOffset
  1482.     dec  word ptr Height
  1483.     jnz  @@FillRowsLoop
  1484. @@FillDone:
  1485.     mov  dx,GCINDEX+1
  1486.     mov  al,0ffh
  1487.     out  dx,al
  1488.     ret
  1489.  
  1490. XRectPattern ENDP
  1491.  
  1492. XCpVidPage PROC FAR SourceOffs : Word, DestOffs : Word
  1493.  
  1494. LOCAL ScreenSize : Word
  1495.  
  1496.     push    ds
  1497.     cld
  1498.     mov     ax, ScrnLogicalByteWidth
  1499.     mov     bx, ScrnLogicalHeight
  1500.     mul     bx
  1501.     mov     ScreenSize, ax
  1502.     mov     ax, SCREENSEG
  1503.     mov     ds, ax
  1504.     mov     es, ax
  1505.  
  1506. @@PlaneLoop:
  1507.  
  1508.     mov     si, SourceOffs
  1509.     mov     di, DestOffs
  1510.  
  1511.     mov     ax,BITMASK
  1512.     mov     dx,GCINDEX
  1513.     out     dx,ax
  1514.     mov     dx,SCINDEX+1
  1515.     mov     al, 00fh
  1516.     out     dx, al
  1517.  
  1518.     mov     cx,ScreenSize
  1519.  
  1520.     rep     movsb
  1521.  
  1522.     mov     dx,GCINDEX+1
  1523.     mov     al,0ffh
  1524.     out     dx,al
  1525.     pop     ds
  1526.     ret
  1527.  
  1528. XCpVidPage ENDP
  1529.  
  1530. XCpVidRect PROC FAR SrcStartX : Word, SrcStartY : Word, SrcEndX : Word,\
  1531.                                         SrcEndY : Word, DestStartX : Word, DestStartY : Word,\
  1532.                                         SrcPageBase : Word, DestPageBase : Word,\
  1533.                                         SrcBitmapW : Word, DestBitmapW : Word
  1534. LOCAL SrcNextOffs : Word, DestNextOffs : Word, RectAddrW : Word, Height : Word
  1535.  
  1536.     push    ds
  1537.     cld
  1538.     mov     dx,GCINDEX
  1539.     mov     ax,BITMASK
  1540.     out     dx,ax
  1541.     mov     ax,SCREENSEG
  1542.     mov     es,ax
  1543.     mov     ax,DestBitmapW
  1544.     shr     ax,2
  1545.     mul     DestStartY
  1546.     mov     di,DestStartX
  1547.     shr     di,2
  1548.     add     di,ax
  1549.     add     di,DestPageBase
  1550.     mov     ax,SrcBitmapW
  1551.     shr     ax,2
  1552.     mul     SrcStartY
  1553.     mov     si,SrcStartX
  1554.     mov     bx,si
  1555.     shr     si,2
  1556.     add     si,ax
  1557.     add     si,SrcPageBase
  1558.     and     bx,0003h
  1559.     mov     ah,byte ptr LeftClipPlaneMask[bx]
  1560.     mov     bx,SrcEndX
  1561.     and     bx,0003h
  1562.     mov     al,byte ptr RightClipPlaneMask[bx]
  1563.     mov     bx,ax
  1564.     mov     cx,SrcEndX
  1565.     mov     ax,SrcStartX
  1566.     cmp     cx,ax
  1567.     jle     @@CopyDone
  1568.     dec     cx
  1569.     and     ax,not 011b
  1570.     sub     cx,ax
  1571.     shr     cx,2
  1572.     jnz     @@MasksSet
  1573.     and     bh,bl
  1574. @@MasksSet:
  1575.     mov     ax,SrcEndY
  1576.     sub     ax,SrcStartY
  1577.     jle     @@CopyDone
  1578.     mov     Height,ax
  1579.     mov     ax,DestBitmapW
  1580.     shr     ax,2
  1581.     sub     ax,cx
  1582.     dec     ax
  1583.     mov     DestNextOffs,ax
  1584.     mov     ax,SrcBitmapW
  1585.     shr     ax,2
  1586.     sub     ax,cx
  1587.     dec     ax
  1588.     mov     SrcNextOffs,ax
  1589.     mov     RectAddrW,cx
  1590.     mov     dx,SCINDEX+1
  1591.     mov     ax,es
  1592.     mov     ds,ax
  1593. @@CopyRowsLoop:
  1594.     mov     cx,RectAddrW
  1595.     mov     al,bh
  1596.     out     dx,al
  1597.     movsb
  1598.     dec     cx
  1599.     js      @@CopyLoopBottom
  1600.     jz      @@DoRightEdge
  1601.     mov     al,00fh
  1602.     out     dx,al
  1603.     rep     movsb
  1604. @@DoRightEdge:
  1605.     mov     al,bl
  1606.     out     dx,al
  1607.     movsb
  1608. @@CopyLoopBottom:
  1609.     add     si,SrcNextOffs
  1610.     add     di,DestNextOffs
  1611.     dec     word ptr Height
  1612.     jnz     @@CopyRowsLoop
  1613. @@CopyDone:
  1614.     mov     dx,GCINDEX+1
  1615.     mov     al,0ffh
  1616.     out     dx,al
  1617.     pop     ds
  1618.     ret
  1619.  
  1620. XCpVidRect ENDP
  1621.  
  1622. XShiftRect PROC FAR SrcLeft : Word, SrcTop : Word, SrcRight : Word,\
  1623.                                         SrcBottom : Word, DestLeft : Word, DestTop : Word,\
  1624.                                         ScreenOffs : Word
  1625. LOCAL widthtemp : Word
  1626.  
  1627.     push ds
  1628.     mov si, SrcLeft
  1629.     shr si, 2
  1630.  
  1631.     mov di, DestLeft
  1632.     shr di, 2
  1633.  
  1634.     mov bx, SrcRight
  1635.     add bx, 3
  1636.     shr bx, 2
  1637.     sub bx, si
  1638.     mov ax, bx
  1639.     inc bx
  1640.     mov widthtemp, bx
  1641.  
  1642.     cld
  1643.  
  1644.     cmp si, di
  1645.     jge @@MovingLeft
  1646.  
  1647.     add si, ax
  1648.     add di, ax
  1649.     neg bx
  1650.     std
  1651.  
  1652. @@MovingLeft:
  1653.  
  1654.     mov cx, ScrnLogicalByteWidth
  1655.     mov ax, SrcTop
  1656.     mov dx, DestTop
  1657.     cmp ax, dx
  1658.     jge @@MovingUp
  1659.  
  1660.     mov ax, SrcBottom
  1661.     add dx, ax
  1662.     sub dx, SrcTop
  1663.     neg cx
  1664.  
  1665. @@MovingUp:
  1666.     push dx
  1667.     mul ScrnLogicalByteWidth
  1668.     add si, ax
  1669.     pop ax
  1670.     mul ScrnLogicalByteWidth
  1671.     add di, ax
  1672.  
  1673.     sub cx, bx
  1674.  
  1675.     add si, ScreenOffs
  1676.     add di, ScreenOffs
  1677.  
  1678.     mov dx, SCINDEX
  1679.     mov ax, 00f02h
  1680.     out dx, ax
  1681.  
  1682.     mov dx, GCINDEX
  1683.     mov ax, BITMASK
  1684.     out dx, ax
  1685.  
  1686.     mov ax, SCREENSEG
  1687.     mov es, ax
  1688.     mov ds, ax
  1689.  
  1690.     mov ah, byte ptr widthtemp
  1691.  
  1692.     mov bx, SrcBottom
  1693.     sub bx, SrcTop
  1694.  
  1695.     mov dx, cx
  1696.  
  1697.     xor ch, ch
  1698.  
  1699. @@LineLoop:
  1700.     mov cl, ah
  1701.     rep movsb
  1702.  
  1703.     add si, dx
  1704.     add di, dx
  1705.  
  1706.     dec bx
  1707.     jge @@LineLoop
  1708.  
  1709.     mov dx, GCINDEX + 1
  1710.     mov al, 0ffh
  1711.     out dx, al
  1712.     pop ds
  1713.     ret
  1714.  
  1715. XShiftRect ENDP
  1716.  
  1717. XCircle PROC FAR Left : Word, Top : Word, Diameter : Word, Color : Word,\
  1718.                                  ScreenOffs : Word
  1719. LOCAL offset0 : Word, offset1 : Word, offset2 : Word, offset3 : Word,\
  1720.             offset4 : Word, offset5 : Word, offset6 : Word, offset7 : Word,\
  1721.             mask0n1 : Word, mask2n3 : Word, mask4n5 : Word, mask6n7 : Word,\
  1722.             shrunkradius : Word, diametereven : Word, Aerror : Word
  1723.  
  1724.     push ds
  1725.     mov di, ScrnLogicalByteWidth
  1726.     xor dx, dx
  1727.     mov ax, Diameter
  1728.     dec ax
  1729.     shr ax, 1
  1730.     adc dx, 0
  1731.     mov shrunkradius, ax
  1732.     mov diametereven, dx
  1733.     add ax, Top
  1734.     mul di
  1735.     add ax, ScreenOffs
  1736.  
  1737.     mov bx, Left
  1738.     mov cx, bx
  1739.     mov si, bx
  1740.     shr si, 2
  1741.     add si, ax
  1742.     mov offset6, si
  1743.     and bx, 3
  1744.     mov bl, byte ptr ColumnMask[bx]
  1745.     mov mask6n7, bx
  1746.  
  1747.     add cx, Diameter
  1748.     dec cx
  1749.     mov bx, cx
  1750.     shr cx, 2
  1751.     add cx, ax
  1752.     mov offset2, cx
  1753.     and bx, 3
  1754.     mov bl, byte ptr ColumnMask[bx]
  1755.     mov mask2n3, bx
  1756.  
  1757.     cmp diametereven, 1
  1758.     jne @@MiddlePlotsOverlap
  1759.     add si, di
  1760.     add cx, di
  1761. @@MiddlePlotsOverlap:
  1762.     mov offset7, si
  1763.     mov offset3, cx
  1764.  
  1765.     mov bx, Left
  1766.     add bx, shrunkradius
  1767.  
  1768.     mov ax, Top
  1769.     mul di
  1770.     add ax, ScreenOffs
  1771.     mov si, ax
  1772.  
  1773.     mov ax, Diameter
  1774.     dec ax
  1775.     mul di
  1776.     add ax, si
  1777.  
  1778.     mov di, bx
  1779.     shr di, 2
  1780.     add si, di
  1781.     mov offset4, si
  1782.     add di, ax
  1783.     mov offset5, di
  1784.     and bx, 3
  1785.     mov bl, byte ptr ColumnMask[bx]
  1786.     mov mask4n5, bx
  1787.  
  1788.     cmp diametereven, 1
  1789.     jne @@TopAndBottomPlotsOverlap
  1790.     rol bl, 1
  1791.     jnc @@TopAndBottomPlotsOverlap
  1792.     inc si
  1793.     inc di
  1794. @@TopAndBottomPlotsOverlap:
  1795.     mov offset0, si
  1796.     mov offset1, di
  1797.     mov mask0n1, bx
  1798.     mov bx, ScrnLogicalByteWidth
  1799.  
  1800.     mov dx, SCREENSEG
  1801.     mov ds, dx
  1802.  
  1803.     mov dx, SCINDEX
  1804.     mov al, MAPMASK
  1805.     out dx, al
  1806.     inc dx
  1807.  
  1808.     mov si, Diameter
  1809.     inc si
  1810.  
  1811.     mov cx, si
  1812.     neg cx
  1813.     add cx, 2
  1814.     mov Aerror, cx
  1815.  
  1816.     xor cx, cx
  1817.     mov ah, byte ptr Color
  1818.     jmp @@CircleCalc
  1819.  
  1820. @@NoAdvance:
  1821.     mov al, byte ptr mask0n1
  1822.     out dx, al
  1823.     mov di, offset0
  1824.     mov [di], ah
  1825.     rol al, 1
  1826.     mov byte ptr mask0n1, al
  1827.     adc di, 0
  1828.     mov offset0, di
  1829.     mov di, offset1
  1830.     mov [di], ah
  1831.     ror al, 1
  1832.     adc di, 0
  1833.     mov offset1, di
  1834.  
  1835.     mov al, byte ptr mask2n3
  1836.     out dx, al
  1837.     mov di, offset2
  1838.     mov [di], ah
  1839.     sub di, bx
  1840.     mov offset2, di
  1841.     mov di, offset3
  1842.     mov [di], ah
  1843.     add di, bx
  1844.     mov offset3, di
  1845.  
  1846.     mov al, byte ptr mask4n5
  1847.     out dx, al
  1848.     mov di, offset4
  1849.     mov [di], ah
  1850.     ror al, 1
  1851.     mov byte ptr mask4n5, al
  1852.     sbb di, 0
  1853.     mov offset4, di
  1854.     mov di, offset5
  1855.     mov [di], ah
  1856.     rol al, 1
  1857.     sbb di, 0
  1858.     mov offset5, di
  1859.  
  1860.     mov al, byte ptr mask6n7
  1861.     out dx, al
  1862.     mov di, offset6
  1863.     mov [di], ah
  1864.     sub di, bx
  1865.     mov offset6, di
  1866.     mov di, offset7
  1867.     mov [di], ah
  1868.     add di, bx
  1869.     mov offset7, di
  1870.  
  1871.     jmp @@CircleCalc
  1872.  
  1873. @@Advance:
  1874.     mov al, byte ptr mask0n1
  1875.     out dx, al
  1876.     mov di, offset0
  1877.     mov [di], ah
  1878.     rol al, 1
  1879.     mov byte ptr mask0n1, al
  1880.     adc di, bx
  1881.     mov offset0, di
  1882.     mov di, offset1
  1883.     mov [di], ah
  1884.     ror al, 1
  1885.     adc di, 0
  1886.     sub di, bx
  1887.     mov offset1, di
  1888.  
  1889.     mov al, byte ptr mask2n3
  1890.     out dx, al
  1891.     mov di, offset2
  1892.     mov [di], ah
  1893.     ror al, 1
  1894.     mov byte ptr mask2n3, al
  1895.     sbb di, bx
  1896.     mov offset2, di
  1897.     mov di, offset3
  1898.     mov [di], ah
  1899.     rol al, 1
  1900.     sbb di, 0
  1901.     add di, bx
  1902.     mov offset3, di
  1903.  
  1904.     mov al, byte ptr mask4n5
  1905.     out dx, al
  1906.     mov di, offset4
  1907.     mov [di], ah
  1908.     ror al, 1
  1909.     mov byte ptr mask4n5, al
  1910.     sbb di, 0
  1911.     add di, bx
  1912.     mov offset4, di
  1913.     mov di, offset5
  1914.     mov [di], ah
  1915.     rol al, 1
  1916.     sbb di, bx
  1917.     mov offset5, di
  1918.  
  1919.     mov al, byte ptr mask6n7
  1920.     out dx, al
  1921.     mov di, offset6
  1922.     mov [di], ah
  1923.     rol al, 1
  1924.     mov byte ptr mask6n7, al
  1925.     adc di, 0
  1926.     sub di, bx
  1927.     mov offset6, di
  1928.     mov di, offset7
  1929.     mov [di], ah
  1930.     ror al, 1
  1931.     adc di, bx
  1932.     mov offset7, di
  1933.  
  1934. @@CircleCalc:
  1935.     add cx, 2
  1936.     mov di, Aerror
  1937.     add di, cx
  1938.     inc di
  1939.     jl @@CircleNoError
  1940.     cmp cx, si
  1941.     ja @@FleeFlyFlowFum
  1942.     sub si, 2
  1943.     sub di, si
  1944.     mov Aerror, di
  1945.     jmp @@Advance
  1946. @@CircleNoError:
  1947.     mov Aerror, di
  1948.     jmp @@NoAdvance
  1949.  
  1950. @@FleeFlyFlowFum:
  1951.     pop ds
  1952.     ret
  1953.  
  1954. XCircle ENDP
  1955.  
  1956. XFilledCircle PROC FAR Left : Word, Top : Word, Diameter : Word,\
  1957.                                              Color : Word, ScreenOffs : Word
  1958. LOCAL offset0 : Word, offset1 : Word, offset2 : Word, offset3 : Word,\
  1959.             offset4 : Word, offset5 : Word, offset6 : Word, offset7 : Word,\
  1960.             mask0n1 : Word, mask2n3 : Word, mask4n5 : Word, mask6n7 : Word,\
  1961.             shrunkradius : Word, diametereven : Word, Aerror : Word,\
  1962.             jumpvector : Word
  1963.  
  1964.     cld
  1965.     mov di, ScrnLogicalByteWidth
  1966.     xor dx, dx
  1967.     mov ax, Diameter
  1968.     dec ax
  1969.     shr ax, 1
  1970.     adc dx, 0
  1971.     mov shrunkradius, ax
  1972.     mov diametereven, dx
  1973.     add ax, Top
  1974.     mul di
  1975.     add ax, ScreenOffs
  1976.  
  1977.     mov bx, Left
  1978.     mov cx, bx
  1979.     mov si, bx
  1980.     shr si, 2
  1981.     add si, ax
  1982.     mov offset6, si
  1983.     and bx, 3
  1984.     mov bl, byte ptr ColumnMask[bx]
  1985.     mov mask6n7, bx
  1986.  
  1987.     add cx, Diameter
  1988.     dec cx
  1989.     mov bx, cx
  1990.     shr cx, 2
  1991.     add cx, ax
  1992.     mov offset2, cx
  1993.     and bx, 3
  1994.     mov bl, byte ptr ColumnMask[bx]
  1995.     mov mask2n3, bx
  1996.  
  1997.     cmp diametereven, 1
  1998.     jne @@MiddlePlotsOverlap
  1999.     add si, di
  2000.     add cx, di
  2001. @@MiddlePlotsOverlap:
  2002.     mov offset7, si
  2003.     mov offset3, cx
  2004.  
  2005.     mov bx, Left
  2006.     add bx, shrunkradius
  2007.  
  2008.     mov ax, Top
  2009.     mul di
  2010.     add ax, ScreenOffs
  2011.     mov si, ax
  2012.  
  2013.     mov ax, Diameter
  2014.     dec ax
  2015.     mul di
  2016.     add ax, si
  2017.  
  2018.     mov di, bx
  2019.     shr di, 2
  2020.     add si, di
  2021.     mov offset4, si
  2022.     add di, ax
  2023.     mov offset5, di
  2024.     and bx, 3
  2025.     mov bl, byte ptr ColumnMask[bx]
  2026.     mov mask4n5, bx
  2027.  
  2028.     cmp diametereven, 1
  2029.     jne @@TopAndBottomPlotsOverlap
  2030.     rol bl, 1
  2031.     jnc @@TopAndBottomPlotsOverlap
  2032.     inc si
  2033.     inc di
  2034. @@TopAndBottomPlotsOverlap:
  2035.     mov offset0, si
  2036.     mov offset1, di
  2037.     mov mask0n1, bx
  2038.  
  2039.     mov bx, ScrnLogicalByteWidth
  2040.  
  2041.     mov dx, SCREENSEG
  2042.     mov es, dx
  2043.  
  2044.     mov dx, SCINDEX
  2045.     mov al, MAPMASK
  2046.     out dx, al
  2047.     inc dx
  2048.     mov si, Diameter
  2049.     inc si
  2050.  
  2051.     mov cx, si
  2052.     neg cx
  2053.     add cx, 2
  2054.     mov Aerror, cx
  2055.     xor cx, cx
  2056.     mov ah, byte ptr Color
  2057.     jmp @@FilledCircleCalc
  2058. @@PlotLines:
  2059.     push cx
  2060.  
  2061.     mov di, mask0n1
  2062.     and di, 0000fh
  2063.     mov al, byte ptr RightMaskTable[di]
  2064.     mov di, offset0
  2065.     cmp di, offset4
  2066.     jne @@PlotXMajorNontrivial
  2067.     mov di, mask4n5
  2068.     and di, 0000fh
  2069.     and al, byte ptr LeftMaskTable[di]
  2070.     out dx, al
  2071.     mov di, offset4
  2072.     mov es:[di], ah
  2073.     mov di, offset5
  2074.     mov es:[di], ah
  2075.     jmp @@PlotYMajor
  2076. @@PlotXMajorNontrivial:
  2077.     out dx, al
  2078.     mov es:[di], ah
  2079.     mov di, offset1
  2080.     mov es:[di], ah
  2081.  
  2082.     mov di, mask4n5
  2083.     and di, 0000fh
  2084.     mov al, byte ptr LeftMaskTable[di]
  2085.     out dx, al
  2086.     mov di, offset4
  2087.     mov es:[di], ah
  2088.     mov di, offset5
  2089.     mov es:[di], ah
  2090.  
  2091.     mov al, 0ffh
  2092.     out dx, al
  2093.     mov al, ah
  2094.     inc di
  2095.     mov cx, offset1
  2096.     sub cx, di
  2097.     push cx
  2098.     shr cx, 1
  2099.     rep stosw
  2100.     adc cx, 0
  2101.     rep stosb
  2102.  
  2103.     mov di, offset4
  2104.     inc di
  2105.     pop cx
  2106.     shr cx, 1
  2107.     rep stosw
  2108.     adc cx, 0
  2109.     rep stosb
  2110.  
  2111. @@PlotYMajor:
  2112.     mov di, mask2n3
  2113.     and di, 0000fh
  2114.     mov al, byte ptr RightMaskTable[di]
  2115.     mov di, offset2
  2116.     cmp di, offset6
  2117.     jne @@PlotYMajorNontrivial
  2118.     mov di, mask6n7
  2119.     and di, 0000fh
  2120.     and al, byte ptr LeftMaskTable[di]
  2121.     out dx, al
  2122.     mov di, offset6
  2123.     mov es:[di], ah
  2124.     mov di, offset7
  2125.     mov es:[di], ah
  2126.     jmp @@ClimaxOfPlot
  2127. @@PlotYMajorNontrivial:
  2128.     out dx, al
  2129.     mov es:[di], ah
  2130.     mov di, offset3
  2131.     mov es:[di], ah
  2132.  
  2133.     mov di, mask6n7
  2134.     and di, 0000fh
  2135.     mov al, byte ptr LeftMaskTable[di]
  2136.     out dx, al
  2137.     mov di, offset6
  2138.     mov es:[di], ah
  2139.     mov di, offset7
  2140.     mov es:[di], ah
  2141.  
  2142.     mov al, 0ffh
  2143.     out dx, al
  2144.     mov al, ah
  2145.  
  2146.     inc di
  2147.     mov cx, offset3
  2148.     sub cx, di
  2149.     push cx
  2150.     shr cx, 1
  2151.     rep stosw
  2152.     adc cx, 0
  2153.     rep stosb
  2154.  
  2155.     mov di, offset6
  2156.     inc di
  2157.     pop cx
  2158.     shr cx, 1
  2159.     rep stosw
  2160.     adc cx, 0
  2161.     rep stosb
  2162.  
  2163. @@ClimaxOfPlot:
  2164.     pop cx
  2165.     jmp [jumpvector]
  2166.  
  2167.  
  2168. @@NoAdvance:
  2169.     mov al, byte ptr mask0n1
  2170.     mov di, offset0
  2171.     rol al, 1
  2172.     mov byte ptr mask0n1, al
  2173.     adc di, 0
  2174.     mov offset0, di
  2175.     mov di, offset1
  2176.     ror al, 1
  2177.     adc di, 0
  2178.     mov offset1, di
  2179.  
  2180.     mov al, byte ptr mask4n5
  2181.     mov di, offset4
  2182.     ror al, 1
  2183.     mov byte ptr mask4n5, al
  2184.     sbb di, 0
  2185.     mov offset4, di
  2186.     mov di, offset5
  2187.     rol al, 1
  2188.     sbb di, 0
  2189.     mov offset5, di
  2190.  
  2191.     mov al, byte ptr mask2n3
  2192.     mov di, offset2
  2193.     sub di, bx
  2194.     mov offset2, di
  2195.     mov di, offset3
  2196.     add di, bx
  2197.     mov offset3, di
  2198.  
  2199.     mov al, byte ptr mask6n7
  2200.     mov di, offset6
  2201.     sub di, bx
  2202.     mov offset6, di
  2203.     mov di, offset7
  2204.     add di, bx
  2205.     mov offset7, di
  2206.  
  2207.     jmp @@FilledCircleCalc
  2208.  
  2209. @@Advance:
  2210.     mov al, byte ptr mask0n1
  2211.     mov di, offset0
  2212.     rol al, 1
  2213.     mov byte ptr mask0n1, al
  2214.     adc di, bx
  2215.     mov offset0, di
  2216.     mov di, offset1
  2217.     ror al, 1
  2218.     adc di, 0
  2219.     sub di, bx
  2220.     mov offset1, di
  2221.  
  2222.     mov al, byte ptr mask2n3
  2223.     mov di, offset2
  2224.     ror al, 1
  2225.     mov byte ptr mask2n3, al
  2226.     sbb di, bx
  2227.     mov offset2, di
  2228.     mov di, offset3
  2229.     rol al, 1
  2230.     sbb di, 0
  2231.     add di, bx
  2232.     mov offset3, di
  2233.  
  2234.     mov al, byte ptr mask4n5
  2235.     mov di, offset4
  2236.     ror al, 1
  2237.     mov byte ptr mask4n5, al
  2238.     sbb di, 0
  2239.     add di, bx
  2240.     mov offset4, di
  2241.     mov di, offset5
  2242.     rol al, 1
  2243.     sbb di, bx
  2244.     mov offset5, di
  2245.  
  2246.     mov al, byte ptr mask6n7
  2247.     mov di, offset6
  2248.     rol al, 1
  2249.     mov byte ptr mask6n7, al
  2250.     adc di, 0
  2251.     sub di, bx
  2252.     mov offset6, di
  2253.     mov di, offset7
  2254.     ror al, 1
  2255.     adc di, bx
  2256.     mov offset7, di
  2257.  
  2258. @@FilledCircleCalc:
  2259.     add cx, 2
  2260.     mov di, Aerror
  2261.     add di, cx
  2262.     inc di
  2263.     jl @@FilledCircleNoError
  2264.     cmp cx, si
  2265.     ja @@FleeFlyFlowFum
  2266.     sub si, 2
  2267.     sub di, si
  2268.     mov Aerror, di
  2269.     mov jumpvector, offset @@Advance
  2270.     jmp @@PlotLines
  2271. @@FilledCircleNoError:
  2272.     mov Aerror, di
  2273.     mov jumpvector, offset @@NoAdvance
  2274.     jmp @@PlotLines
  2275.  
  2276. @@FleeFlyFlowFum:
  2277.     ret
  2278.  
  2279. XFilledCircle ENDP
  2280.  
  2281. XGetPalStruc PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2282.  
  2283.     cld
  2284.     les  di,PalBuff
  2285.     mov  si,StartColor
  2286.     mov  ax,si
  2287.     stosb
  2288.     mov  dx,NumColors
  2289.     mov  al,dl
  2290.     stosb
  2291.     mov  cx,dx
  2292.     cld
  2293.     cmp  VSyncHandlerActive,1
  2294.     je   @@HandleVVH
  2295.     mov  dx,INPUTSTATUS0
  2296. @@WaitNotVsync:
  2297.     in   al,dx
  2298.     test al,08h
  2299.     jnz  @@WaitNotVsync
  2300. @@WaitVsync:
  2301.     in   al,dx
  2302.     test al,08h
  2303.     jz   @@WaitVsync
  2304.     jmp  short @@SkipVSync
  2305. @@HandleVVH:
  2306.     cmp  StartAddressFlag,0
  2307.     jne  @@HandleVVH
  2308. @@SkipVSync:
  2309.     mov  ax, si
  2310.     mov  dx, DACREADINDEX
  2311.     cli
  2312.     out  dx, al
  2313.     mov  dx, DACDATA
  2314.     mov  bx, cx
  2315.     shl  bx, 1
  2316.     add  cx, bx
  2317.     rep  insb
  2318.     sti
  2319.     ret
  2320.  
  2321. XGetPalStruc ENDP
  2322.  
  2323. XGetPalRaw PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2324.  
  2325.     les  di,PalBuff
  2326.     mov  si,StartColor
  2327.     mov  cx,NumColors
  2328. @@ReadPalEntry:
  2329.     cld
  2330.     cmp  VSyncHandlerActive,1
  2331.     je   @@HandleVVH
  2332.     mov  dx,INPUTSTATUS0
  2333. @@WaitNotVsync:
  2334.     in   al,dx
  2335.     test al,08h
  2336.     jnz  @@WaitNotVsync
  2337. @@WaitVsync:
  2338.     in   al,dx
  2339.     test al,08h
  2340.     jz   @@WaitVsync
  2341.     jmp  short @@SkipVSync
  2342. @@HandleVVH:
  2343.     cmp  StartAddressFlag,0
  2344.     jne  @@HandleVVH
  2345. @@SkipVSync:
  2346.  
  2347.     mov  ax,si
  2348.     mov  dx,DACREADINDEX
  2349.     cli
  2350.     out  dx,al
  2351.     mov  dx,DACDATA
  2352.     mov  bx,cx
  2353.     shl  bx,1
  2354.     add  cx,bx
  2355.     rep  insb
  2356.     sti
  2357.     ret
  2358.  
  2359. XGetPalRaw ENDP
  2360.  
  2361.  
  2362. XPutPalStruc PROC FAR CompPalBuff : DWord
  2363.  
  2364.     push ds
  2365.     cld
  2366.     lds  si,CompPalBuff
  2367.     lodsb
  2368.     mov  ah,0
  2369.     mov  bx,ax
  2370.     lodsb
  2371.     mov  ah,0
  2372.     mov  cx,ax
  2373.  
  2374.     mov  ax, @data
  2375.     mov  es, ax
  2376.     cmp  es:[VSyncHandlerActive], TRUE
  2377.     jne  @@NoVSyncHandler
  2378.  
  2379. @@WaitForLast:
  2380.     cmp  es:[VsyncPaletteCount],0
  2381.     jne  @@WaitForLast
  2382.     push cx
  2383.     push es
  2384.     mov  di, offset VsyncPaletteBuffer
  2385.     mov  ax,3
  2386.     mul  cx
  2387.     mov  cx,ax
  2388.     rep  movsb
  2389.     pop  ds
  2390.     pop  cx
  2391.     mov  VsyncPaletteStart,bx
  2392.     mov  VsyncPaletteCount,cx
  2393.     jmp  short @@Done
  2394.  
  2395. @@NoVsyncHandler:
  2396.     or   cx,cx
  2397.     jz   @@Done
  2398.     ;cli
  2399.     cld
  2400.     mov  dx,INPUTSTATUS0
  2401. @@WaitNotVsync:
  2402.     in   al,dx
  2403.     test al,08h
  2404.     jnz  @@WaitNotVsync
  2405. @@WaitVsync:
  2406.     in   al,dx
  2407.     test al,08h
  2408.     jz   @@WaitVsync
  2409.  
  2410.     ;new stuff
  2411.     mov  ax,3
  2412.     mul  cx
  2413.     mov  cx,ax
  2414.     ;end new stuff
  2415.  
  2416.     mov  ax,bx
  2417.     mov  bx,60
  2418. @@SetLoop:
  2419.     mov  dx,DACWRITEINDEX
  2420.     out  dx,al
  2421.     mov  dx,DACDATA
  2422.  
  2423.     rep outsb
  2424.  
  2425. ;    outsb
  2426. ;    outsb
  2427. ;    outsb
  2428. ;    inc  al
  2429. ;    dec  bx
  2430. ;    js   @@testvsync
  2431. ;    loop @@SetLoop
  2432.     jmp  @@Done
  2433. @@testvsync:
  2434.     mov     dx,INPUTSTATUS0
  2435.     push    ax
  2436. @@Wait:
  2437.     in      al,dx
  2438.     test    al,08h
  2439.     jz      @@Wait
  2440.     pop     ax
  2441.     mov     bx,60
  2442.     loop @@SetLoop
  2443. @@Done:
  2444.     sti
  2445.     pop  ds
  2446.     ret
  2447.  
  2448. XPutPalStruc ENDP
  2449.  
  2450. XTransposePalStruc PROC FAR CompPalBuff : DWord, StartColor : Word
  2451.  
  2452.     push ds
  2453.     cld
  2454.     lds  si,CompPalBuff
  2455.     mov  bx,StartColor
  2456.     mov  [si],bl
  2457.     inc  si
  2458.     lodsb
  2459.     mov  ah,0
  2460.     mov cx,ax
  2461.  
  2462.     mov  ax, @data
  2463.     mov  es, ax
  2464.     cmp  es:[VSyncHandlerActive], TRUE
  2465.     jne  @@NoVSyncHandler
  2466.  
  2467. @@WaitForLast:
  2468.     cmp  es:[VsyncPaletteCount],0
  2469.     jne  @@WaitForLast
  2470.     push cx
  2471.     push es
  2472.     mov  di, offset VsyncPaletteBuffer
  2473.     mov  ax,3
  2474.     mul  cx
  2475.     mov  cx,ax
  2476.     rep  movsb
  2477.     pop  ds
  2478.     pop  cx
  2479.     mov  VsyncPaletteStart,bx
  2480.     mov  VsyncPaletteCount,cx
  2481.     jmp  short @@Done
  2482.  
  2483. @@NoVsyncHandler:
  2484.     or   cx,cx
  2485.     jz   @@Done
  2486.     ;cli
  2487.     cld
  2488.     mov  dx,INPUTSTATUS0
  2489. @@WaitNotVsync:
  2490.     in   al,dx
  2491.     test al,08h
  2492.     jnz  @@WaitNotVsync
  2493. @@WaitVsync:
  2494.     in   al,dx
  2495.     test al,08h
  2496.     jz   @@WaitVsync
  2497.     mov  ax,bx
  2498.     mov  bx,60
  2499. @@SetLoop:
  2500.     mov  dx,DACWRITEINDEX
  2501.     out  dx,al
  2502.     mov  dx,DACDATA
  2503.     outsb
  2504.     outsb
  2505.     outsb
  2506.     inc  al
  2507. ;    dec  bx
  2508. ;    js   @@testvsync
  2509.     loop @@SetLoop
  2510.     jmp  @@Done
  2511. @@testvsync:
  2512.     mov     dx,INPUTSTATUS0
  2513.     push    ax
  2514. @@Wait:
  2515.     in      al,dx
  2516.     test    al,08h
  2517.     jz      @@Wait
  2518.     pop     ax
  2519.     mov     bx,60
  2520.     loop @@SetLoop
  2521. @@Done:
  2522.     sti
  2523.     pop  ds
  2524.     ret
  2525.  
  2526. XTransposePalStruc ENDP
  2527.  
  2528.  
  2529. XPutPalRaw PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2530.  
  2531.     push ds
  2532.     mov  cx,NumColors
  2533.     mov  bx,StartColor
  2534.     lds  si,PalBuff
  2535.  
  2536. @@WritePalEntry:
  2537.     mov  ax, @data
  2538.     mov  es, ax
  2539.     cmp  es:[VSyncHandlerActive], TRUE
  2540.     jne  @@NoVSyncHandler
  2541.  
  2542. @@WaitForLast:
  2543.     cmp  es:[VsyncPaletteCount],0
  2544.     jne  @@WaitForLast
  2545.     push cx
  2546.     push es
  2547.     mov  di, offset VsyncPaletteBuffer
  2548.     mov  ax,3
  2549.     mul  cx
  2550.     mov  cx,ax
  2551.     rep  movsb
  2552.     pop  ds
  2553.     pop  cx
  2554.     mov  VsyncPaletteStart,bx
  2555.     mov  VsyncPaletteCount,cx
  2556.     jmp  short @@Done
  2557.  
  2558. @@NoVsyncHandler:
  2559.     or   cx,cx
  2560.     jz   @@Done
  2561.     cld
  2562.     mov  dx,INPUTSTATUS0
  2563. @@WaitNotVsync:
  2564.     in   al,dx
  2565.     test al,08h
  2566.     jnz  @@WaitNotVsync
  2567. @@WaitVsync:
  2568.     in   al,dx
  2569.     test al,08h
  2570.     jz   @@WaitVsync
  2571.  
  2572.     ;new stuff
  2573.     mov  ax,3
  2574.     mul  cx
  2575.     mov  cx,ax
  2576.     ;end new stuff
  2577.  
  2578.     mov  ax,bx
  2579.     mov  bx,60
  2580. @@SetLoop:
  2581.     mov  dx,DACWRITEINDEX
  2582.     out  dx,al
  2583.     mov  dx,DACDATA
  2584.  
  2585.     rep outsb
  2586.  
  2587. ;    outsb
  2588. ;    outsb
  2589. ;    outsb
  2590. ;    inc  al
  2591. ;    dec  bx
  2592. ;    js   @@testvsync
  2593. ;    loop @@SetLoop
  2594.     jmp  @@Done
  2595. @@testvsync:
  2596.     mov     dx,INPUTSTATUS0
  2597.     push    ax
  2598. @@Wait:
  2599.     in      al,dx
  2600.     test    al,08h
  2601.     jz      @@Wait
  2602.     pop     ax
  2603.     mov     bx,60
  2604.     loop @@SetLoop
  2605. @@Done:
  2606.     pop  ds
  2607.     ret
  2608.  
  2609. XPutPalRaw ENDP
  2610.  
  2611.  
  2612. XSetRGB PROC FAR ColorIndex : Byte, R : Byte, G : Byte, B : Byte
  2613.  
  2614.     mov  al,ColorIndex
  2615.     mov  dx,DACWRITEINDEX
  2616.     out  dx,al
  2617.     mov  dx,DACDATA
  2618.     mov  al,R
  2619.     out  dx,al
  2620.     mov  al,G
  2621.     out  dx,al
  2622.     mov  al,B
  2623.     out  dx,al
  2624.     ret
  2625.  
  2626. XSetRGB ENDP
  2627.  
  2628. XRotPalStruc PROC FAR PalBuff : DWord, Direction : Word
  2629.  
  2630.     push ds
  2631.     cld
  2632.     lds  si,PalBuff
  2633.     lodsw
  2634.     xor  ch,ch
  2635.     mov  cl,ah
  2636.     mov  ax,ds
  2637.     mov  es,ax
  2638.     dec  cx
  2639.     mov  bx,cx
  2640.     shl  bx,1
  2641.     add  cx,bx
  2642.     cmp  Direction,0
  2643.     jne  @@forward
  2644.     std
  2645.     add  si,cx
  2646.     add  si,2
  2647. @@forward:
  2648.     mov  ax,si
  2649.     mov  di,ax
  2650.     lodsb
  2651.     mov  dl,al
  2652.     lodsb
  2653.     mov  dh,al
  2654.     lodsb
  2655.     mov  bl,al
  2656.     rep  movsb
  2657.     mov  al,dl
  2658.     stosb
  2659.     mov  al,dh
  2660.     stosb
  2661.     mov  al,bl
  2662.     stosb
  2663.     pop  ds
  2664.     ret
  2665.  
  2666. XRotPalStruc ENDP
  2667.  
  2668. XRotPalRaw PROC FAR PalBuff : DWord, Direction : Word, NumColors : Word
  2669.  
  2670.     push ds
  2671.     cld
  2672.     mov  cx,NumColors
  2673.     lds  si,PalBuff
  2674. @@RotatePalEntry:
  2675.     mov  ax,ds
  2676.     mov  es,ax
  2677.     dec  cx
  2678.     mov  bx,cx
  2679.     shl  bx,1
  2680.     add  cx,bx
  2681.     cmp  Direction,0
  2682.     jne  @@forward
  2683.     std
  2684.     add  si,cx
  2685.     add  si,2
  2686. @@forward:
  2687.     mov  ax,si
  2688.     mov  di,ax
  2689.     lodsb
  2690.     mov  dl,al
  2691.     lodsb
  2692.     mov  dh,al
  2693.     lodsb
  2694.     mov  bl,al
  2695.     rep  movsb
  2696.     mov  al,dl
  2697.     stosb
  2698.     mov  al,dh
  2699.     stosb
  2700.     mov  al,bl
  2701.     stosb
  2702.     pop  ds
  2703.     ret
  2704.  
  2705. XRotPalRaw ENDP
  2706.  
  2707. XCpContrastPalStruc PROC FAR PalSrcBuff : DWord, PalDestBuff : DWord,\
  2708.                                                          Intensity : Byte
  2709.  
  2710.     push ds
  2711.     cld
  2712.     mov  bh,0ffh
  2713.     sub  bh,Intensity
  2714.     and  bh,07fh
  2715.     lds  si,PalSrcBuff
  2716.     les  di,PalDestBuff
  2717.     lodsw
  2718.     stosw
  2719.     xor  ch,ch
  2720.     mov  cl,ah
  2721.     mov  dx,0
  2722. @@MainLoop:
  2723.     lodsw
  2724.     sub  al,bh
  2725.     jns  @@DecrementOKR
  2726.     xor  al,al
  2727. @@DecrementOKR:
  2728.     sub  ah,bh
  2729.     jns  @@DecrementOKG
  2730.     xor  ah,ah
  2731. @@DecrementOKG:
  2732.     or   dx,ax
  2733.     or   dl,ah
  2734.     stosw
  2735.     lodsb
  2736.     sub  al,bh
  2737.     jns  @@DecrementOKB
  2738.     xor  al,al
  2739. @@DecrementOKB:
  2740.     or   dl,al
  2741.     stosb
  2742.     loop @@MainLoop
  2743.     mov  ax,dx
  2744.     pop  ds
  2745.     ret
  2746.  
  2747. XCpContrastPalStruc ENDP
  2748.  
  2749.  
  2750. XPutContrastPalStruc PROC FAR CompPalBuff : DWord, Intensity : Byte
  2751.  
  2752.     push    ds
  2753.     cld
  2754.     mov     bh,0ffh
  2755.     sub     bh,Intensity
  2756.     and     bh,07fh
  2757.     mov     di,40
  2758.     lds     si,CompPalBuff
  2759.     lodsb
  2760.     mov     bl,al
  2761.     lodsb
  2762.     mov     ah,0
  2763.     mov     cx,ax
  2764.     or      cx,cx
  2765.     jz      @@Done
  2766.  
  2767.     mov     dx,INPUTSTATUS0
  2768. @@WaitNotVsync:
  2769.     in      al,dx
  2770.     test    al,08h
  2771.     jnz     @@WaitNotVsync
  2772. @@WaitVsync:
  2773.     in      al,dx
  2774.     test    al,08h
  2775.     jz      @@WaitVsync
  2776.  
  2777. @@MainLoop:
  2778.     mov  al,bl
  2779.     mov  dx,DACWRITEINDEX
  2780.     out  dx,al
  2781.     inc  dx
  2782.     lodsb
  2783.     sub  al,bh
  2784.     jns  @@DecrementOKR
  2785.     xor  al,al
  2786. @@DecrementOKR:
  2787.     out  dx,al
  2788.  
  2789.     lodsb
  2790.     sub  al,bh
  2791.     jns  @@DecrementOKG
  2792.     xor  al,al
  2793. @@DecrementOKG:
  2794.     out  dx,al
  2795.  
  2796.     lodsb
  2797.     sub  al,bh
  2798.     jns  @@DecrementOKB
  2799.     xor  al,al
  2800. @@DecrementOKB:
  2801.     out  dx,al
  2802.  
  2803.     inc  bl
  2804.     dec  di
  2805.     js   @@testvsync
  2806.     loop @@MainLoop
  2807.     jmp  @@Done
  2808.  
  2809.  
  2810. @@testvsync:
  2811.     mov     dx,INPUTSTATUS0
  2812.     push    ax
  2813. @@Wait:
  2814.     in      al,dx
  2815.     test    al,08h
  2816.     jz      @@Wait
  2817.     pop     ax
  2818.     mov     di,40
  2819.     loop    @@MainLoop
  2820. @@Done:
  2821.     ;sti
  2822.     pop  ds
  2823.     ret
  2824.  
  2825. XPutContrastPalStruc ENDP
  2826.  
  2827. XTextInit PROC FAR
  2828.  
  2829.     push bp
  2830.     mov  FontDriverActive,1
  2831.     mov  ax,1130h
  2832.     push ax
  2833.     mov  bh,3
  2834.     int  10h
  2835.     mov  word ptr F8x8Ptr,bp
  2836. IFDEF _DPMI_
  2837.     mov  ax, __C000H
  2838.     mov  es, ax
  2839. ENDIF
  2840.     mov  word ptr F8x8Ptr+2,es
  2841.  
  2842.     mov  word ptr FontPtr,bp
  2843.     mov  word ptr FontPtr+2,es
  2844.  
  2845.     pop  ax
  2846.     mov  bh,2
  2847.     int  10h
  2848.     mov  word ptr F8x14Ptr,bp
  2849. IFDEF _DPMI_
  2850.     mov  ax, __C000H
  2851.     mov  es, ax
  2852. ENDIF
  2853.     mov  word ptr F8x14Ptr+2,es
  2854.  
  2855.  
  2856.     mov  al,8
  2857.     mov  CharHeight,al
  2858.     mov  CharWidth ,al
  2859.  
  2860.     mov  dx,offset MirrorTable
  2861.     mov  MirrorTableOffs,dx
  2862.     pop  bp
  2863.     ret
  2864.  
  2865. XTextInit ENDP
  2866.  
  2867.  
  2868. XSetFont PROC FAR FontID : Word
  2869.  
  2870.     xor  dx,dx
  2871.     mov  cx,FontID
  2872.     cmp  cx,2
  2873.  
  2874.     jne  @@notuserfont
  2875.     mov  ax,word ptr UserFontPtr
  2876.     mov  word ptr FontPtr,ax
  2877.  
  2878.     mov  ax,word ptr UserFontPtr+2
  2879.     mov  word ptr FontPtr+2,ax
  2880.  
  2881.     mov  al,UserChHeight
  2882.     mov  CharHeight,al
  2883.  
  2884.     mov  al,UserChWidth
  2885.     mov  CharWidth,al
  2886.  
  2887.     mov  al,UserFirstCh
  2888.     mov  FirstChar,al
  2889.     jmp  @@done
  2890.  
  2891. @@notuserfont:
  2892.  
  2893.     mov  dx,offset MirrorTable
  2894.     mov  CharWidth,8
  2895.     mov  FirstChar,0
  2896.     cmp  cx,1
  2897.     jne  @@not8x14font
  2898.  
  2899.     mov  ax,word ptr F8x14Ptr
  2900.     mov  word ptr FontPtr,ax
  2901.  
  2902.     mov  ax,word ptr F8x14Ptr+2
  2903.     mov  word ptr FontPtr+2,ax
  2904.  
  2905.     mov  CharHeight,14
  2906.     jmp  @@done
  2907.  
  2908. @@not8x14font:
  2909.     mov  ax,word ptr F8x8Ptr
  2910.     mov  word ptr FontPtr,ax
  2911.  
  2912.     mov  ax,word ptr F8x8Ptr+2
  2913.     mov  word ptr FontPtr+2,ax
  2914.  
  2915.     mov  CharHeight,8
  2916.  
  2917. @@done:
  2918.     mov  MirrorTableOffs,dx
  2919.     ret
  2920.  
  2921. XSetFont ENDP
  2922.  
  2923. XRegisterUserFont PROC FAR FontToRegister : DWord
  2924.  
  2925.     mov  ax,word ptr FontToRegister
  2926.     mov  bx,word ptr FontToRegister+2
  2927.     add  ax,4
  2928.     mov  word ptr UserFontPtr,ax
  2929.     mov  word ptr UserFontPtr+2,bx
  2930.  
  2931.     push ds
  2932.     lds  si,FontToRegister
  2933.     lodsw
  2934.     mov  bx,ax
  2935.     lodsw
  2936.     pop  ds
  2937.     mov  UserChHeight,al
  2938.     mov  UserChWidth,ah
  2939.     mov  UserFirstCh,bl
  2940.     ret
  2941.  
  2942. XRegisterUserFont ENDP
  2943.  
  2944. XGetCharWidth PROC FAR ACh : Byte
  2945.  
  2946.     xor  ah,ah
  2947.     mov  al,CharWidth
  2948.     or   al,al
  2949.     jz   @@NotFixed
  2950.     jmp  @@Done
  2951.  
  2952. @@NotFixed:
  2953.     push si
  2954.     mov  al,CharHeight
  2955.     mov  bx,ax
  2956.     inc  al
  2957.     mov  dl, ACh
  2958.     sub  dl,FirstChar
  2959.     mul  dl
  2960.     add  ax,bx
  2961.     les  si,dword ptr FontPtr
  2962.     add  si,ax
  2963.     xor  ah,ah
  2964.     mov  al,byte ptr es:[si]
  2965. @@Done:
  2966.     ret
  2967.  
  2968. XGetCharWidth ENDP
  2969.  
  2970. XCharPut PROC FAR Chr : Byte, X : Word, Y : Word, ScrnOffs : Word, Color : Word
  2971. LOCAL ScreenInc : Word, Hold : Word
  2972.  
  2973.     push ds
  2974.     cld
  2975.     mov  ax,ScrnLogicalByteWidth
  2976.     mov  bx,ax
  2977.     sub  bx,3
  2978.     mov  ScreenInc,bx
  2979.     mul  Y
  2980.     mov  di,X
  2981.     mov  cx,di
  2982.     shr  di,2
  2983.     add  di,ax
  2984.     add  di,ScrnOffs
  2985.  
  2986.     mov  ax,SCREENSEG
  2987.     mov  es,ax
  2988.  
  2989.     and  cx,3
  2990.  
  2991.     mov  bx,MirrorTableOffs
  2992.     mov  al,CharHeight
  2993.     xor  ah,ah
  2994.     mov  ch,al
  2995.  
  2996.     cmp  CharWidth,0
  2997.     jne  @@NoWidthByte
  2998.     inc  al
  2999.  
  3000. @@NoWidthByte:
  3001.     mov  dl,Chr
  3002.     sub  dl,FirstChar
  3003.     mul  dl
  3004.     lds  si,dword ptr FontPtr
  3005.     add  si,ax
  3006.  
  3007.     mov  dx,SCINDEX
  3008.  
  3009. @@MainLoop:
  3010.  
  3011.     lodsb
  3012.     or   al,al
  3013.     jz   @@NoCharPixels
  3014.  
  3015.     or   bx,bx
  3016.     jz   @@DontMirror
  3017.     push ds
  3018.     mov  dx,seg @data
  3019.     mov  ds,dx
  3020.     xlat
  3021.     pop  ds
  3022.     mov  dx,SCINDEX
  3023.  
  3024. @@DontMirror:
  3025.     xor  ah,ah
  3026.     shl  ax,cl
  3027.     mov  Hold,ax
  3028.  
  3029.     mov  ah,al
  3030.     and  ah,0fh
  3031.     jnz  @@p1
  3032.     inc  di
  3033.     jmp  @@SecondNibble
  3034.  
  3035. @@p1:
  3036.     mov  al,MAPMASK
  3037.     out  dx,ax
  3038.     mov  al,byte ptr Color
  3039.     stosb
  3040.  
  3041. @@SecondNibble:
  3042.     mov  ax,Hold
  3043.     shl  ax,4
  3044.     and  ah,0fh
  3045.     jnz  @@p2
  3046.     inc  di
  3047.     jmp  @@ThirdNibble
  3048.  
  3049. @@p2:
  3050.     mov  al,MAPMASK
  3051.     out  dx,ax
  3052.     mov  al,byte ptr Color
  3053.     stosb
  3054.  
  3055. @@ThirdNibble:
  3056.     mov  ax,Hold
  3057.     and  ah,0fh
  3058.     jnz  @@p3
  3059.     inc  di
  3060.     jmp  @@NextCharRow
  3061.  
  3062. @@p3:
  3063.     mov  al,MAPMASK
  3064.     out  dx,ax
  3065.     mov  al,byte ptr Color
  3066.     stosb
  3067.  
  3068. @@NextCharRow:
  3069.     add  di,ScreenInc
  3070.     dec  ch
  3071.     jnz  @@MainLoop
  3072.  
  3073. @@done:
  3074.     pop  es
  3075.     mov  ah,0
  3076.     mov  al,es:CharWidth
  3077.     or   al,al
  3078.     jnz  @@FixedSpacing
  3079.     lodsb
  3080. @@FixedSpacing:
  3081.  
  3082.     mov  bx,es
  3083.     mov  ds,bx
  3084.     jmp @@TheEnd
  3085.  
  3086.  
  3087. @@NoCharPixels:
  3088.     add  di,3
  3089.     add  di,ScreenInc
  3090.     dec  ch
  3091.     jnz  @@MainLoop
  3092.     jmp  @@done
  3093.  
  3094. @@TheEnd:
  3095.     ret
  3096. XCharPut ENDP
  3097.  
  3098. HLineClipR PROC NEAR
  3099.  
  3100.     push    di
  3101.     cmp     dx,ax
  3102.     jl      @@Invisible
  3103.  
  3104.     cmp     cx,TopClip
  3105.     jl      @@Invisible
  3106.  
  3107.     cmp     cx,BottomClip
  3108.     jg      @@Invisible
  3109.  
  3110.     mov     di,RightClip
  3111.     sal     di,2
  3112.     cmp     ax,di
  3113.     jg      @@Invisible
  3114.     cmp     dx,di
  3115.     jle     @@ClipLeft
  3116.     mov     dx,di
  3117.  
  3118. @@ClipLeft:
  3119.     mov     di,LeftClip
  3120.     sal     di,2
  3121.     cmp     dx,di
  3122.     jl      @@Invisible
  3123.     cmp     ax,di
  3124.     jge     @@DoLine
  3125.     mov     ax,di
  3126.     jmp     @@DoLine
  3127.  
  3128. @@Invisible:
  3129.     pop     di
  3130.     ret
  3131.  
  3132. @@DoLine:
  3133.     pop     di
  3134.     xchg    cx,ax
  3135.     mov     si,dx
  3136.     mul     ScrnLogicalByteWidth
  3137.     mov     dx,si
  3138.     add     ax,di
  3139.     mov     di,cx
  3140.     sar     di,2
  3141.     add     di,ax
  3142.     and     si,03h
  3143.     mov     ah,byte ptr RightClipPlaneMask[si]
  3144.     mov     si,cx
  3145.     and     si,03h
  3146.     mov     al,byte ptr LeftClipPlaneMask[si]
  3147.     cmp     dx,cx
  3148.     jle     @@Invisible2
  3149.     xchg    cx,dx
  3150.     dec     cx
  3151.     and     dx,not 03h
  3152.     sub     cx,dx
  3153.     js      @@Invisible2
  3154.     shr     cx,2
  3155.     jnz     @@MasksSet
  3156.     and     al,ah
  3157. @@MasksSet:
  3158.     mov     dl,bl
  3159.     mov     bx,ax
  3160.     mov     ah,dl
  3161.     mov     dx,SCINDEX+1
  3162.     mov     al,bl
  3163.     out     dx,al
  3164.     mov     al,ah
  3165.     stosb
  3166.     dec     cx
  3167.     js      @@Invisible2
  3168.     jz      @@RightEnd
  3169.     mov     al,0fh
  3170.     out     dx,al
  3171.     mov     al,ah
  3172.     shr     cx,1
  3173.     rep     stosw
  3174.     adc     cx,cx
  3175.     rep     stosb
  3176.  
  3177. @@RightEnd:
  3178.     mov     al,bh
  3179.     out     dx,al
  3180.     mov     al,ah
  3181.     stosb
  3182. @@Invisible2:
  3183.     ret
  3184.  
  3185. HLineClipR ENDP
  3186.  
  3187. XTriangle PROC FAR X0, Y0, X1, Y1, X2, Y2, Color, PageOffset : Word
  3188. LOCAL DX01, DY01, DX02, DY02, DX12, DY12, DP01, DP02, DP12, XA01, XA02, XA12 : Word
  3189.  
  3190.     push    di
  3191.     push    es
  3192.     push    ds
  3193.     mov     ax,X0
  3194.     mov     bx,Y0
  3195.     mov     cx,X1
  3196.     mov     dx,Y1
  3197.     cmp     bx,dx
  3198.     jl      @@triY0lY1
  3199.     je      @@triY0eY1
  3200.     xchg    ax,cx
  3201.     xchg    bx,dx
  3202.  
  3203. @@triY0lY1:
  3204.     cmp     dx,Y2
  3205.     jg      @@tria
  3206.     jmp     @@trisorted
  3207. @@tria:
  3208.     xchg    cx,X2
  3209.     xchg    dx,Y2
  3210.     cmp     bx,dx
  3211.     jge     @@trib
  3212.     jmp     @@trisorted
  3213. @@trib:
  3214.     je      @@tribot
  3215.     xchg    ax,cx
  3216.     xchg    bx,dx
  3217.     jmp     @@trisorted
  3218. @@triY0eY1:
  3219.     cmp     bx,Y2
  3220.     jl      @@tribot
  3221.     jg      @@tric
  3222.     jmp     @@tridone
  3223. @@tric:
  3224.     xchg    ax,X2
  3225.     xchg    bx,Y2
  3226.     jmp     @@trisorted
  3227.  
  3228. @@tribot:
  3229.     cmp     ax,cx
  3230.     jl      @@tribotsorted
  3231.     jg      @@tribota
  3232.     jmp     @@tridone
  3233. @@tribota:
  3234.     xchg    ax,cx
  3235. @@tribotsorted:
  3236.     cmp     bx,BottomClip
  3237.     jle     @@triboty0ok
  3238.     jmp     @@tridone
  3239. @@triboty0ok:
  3240.     mov     si,Y2
  3241.     cmp     si,TopClip
  3242.     jge     @@triboty2ok
  3243.     jmp     @@tridone
  3244. @@triboty2ok:
  3245.     mov     X0,ax
  3246.     mov     Y0,bx
  3247.     mov     X1,cx
  3248.     mov     Y1,dx
  3249.  
  3250.     mov     bx,Y2
  3251.     sub     bx,Y0
  3252.     mov     DY02,bx
  3253.     mov     ax,X2
  3254.     sub     ax,X0
  3255.     mov     DX02,ax
  3256.     mov     cx,ax
  3257.     cwd
  3258.     idiv    bx
  3259.     cmp     cx,0
  3260.     jge     @@tribot02
  3261.     dec     ax
  3262. @@tribot02:
  3263.     mov     XA02,ax
  3264.     imul    bx
  3265.     sub     cx,ax
  3266.     mov     DP02,cx
  3267.  
  3268.     mov     bx,Y2
  3269.     sub     bx,Y1
  3270.     mov     DY12,bx
  3271.     mov     ax,X2
  3272.     sub     ax,X1
  3273.     mov     DX12,ax
  3274.     mov     cx,ax
  3275.     cwd
  3276.     idiv    bx
  3277.     cmp     cx,0
  3278.     jge     @@tribot12
  3279.     dec     ax
  3280. @@tribot12:
  3281.     mov     XA12,ax
  3282.     imul    bx
  3283.     sub     cx,ax
  3284.     mov     DP12,cx
  3285.  
  3286.     mov     ax,0
  3287.     mov     bx,0
  3288.     mov     cx,Y0
  3289.     mov     si,X0
  3290.     mov     di,X1
  3291.     dec     di
  3292. @@tribotloop:
  3293.     inc     cx
  3294.  
  3295.     add     ax,DP02
  3296.     jle     @@tribotshortl
  3297.     sub     ax,DY02
  3298.     inc     si
  3299. @@tribotshortl:
  3300.     add     si,XA02
  3301.  
  3302.     add     bx,DP12
  3303.     jle     @@tribotshortr
  3304.     sub     bx,DY12
  3305.     inc     di
  3306. @@tribotshortr:
  3307.     add     di,XA12
  3308.  
  3309.     push    di
  3310.     push    si
  3311.     cmp     cx,Y2
  3312.     jl      @@tribotloop
  3313.  
  3314.     jmp     @@tridrawlines
  3315.  
  3316.  
  3317. @@trisorted:
  3318.     cmp     bx,BottomClip
  3319.     jle     @@triy0ok
  3320.     jmp     @@tridone
  3321. @@triy0ok:
  3322.     mov     si,Y2
  3323.     cmp     si,TopClip
  3324.     jge     @@triy2ok
  3325.     jmp     @@tridone
  3326. @@triy2ok:
  3327.     mov     X0,ax
  3328.     mov     Y0,bx
  3329.     mov     X1,cx
  3330.     mov     Y1,dx
  3331.  
  3332.     mov     bx,dx
  3333.     sub     bx,Y0
  3334.     mov     DY01,bx
  3335.     mov     ax,X1
  3336.     sub     ax,X0
  3337.     mov     DX01,ax
  3338.     mov     cx,ax
  3339.     cwd
  3340.     idiv    bx
  3341.     cmp     cx,0
  3342.     jge     @@tripsl01
  3343.     dec     ax
  3344. @@tripsl01:
  3345.     mov     XA01,ax
  3346.     imul    bx
  3347.     sub     cx,ax
  3348.     mov     DP01,cx
  3349.  
  3350.     mov     bx,Y2
  3351.     sub     bx,Y0
  3352.     mov     DY02,bx
  3353.     mov     ax,X2
  3354.     sub     ax,X0
  3355.     mov     DX02,ax
  3356.     mov     cx,ax
  3357.     cwd
  3358.     idiv    bx
  3359.     cmp     cx,0
  3360.     jge     @@tripsl02
  3361.     dec     ax
  3362. @@tripsl02:
  3363.     mov     XA02,ax
  3364.     imul    bx
  3365.     sub     cx,ax
  3366.     mov     DP02,cx
  3367.  
  3368.     mov     bx,Y2
  3369.     sub     bx,Y1
  3370.     jle     @@triconstcomputed
  3371.     mov     DY12,bx
  3372.     mov     ax,X2
  3373.     sub     ax,X1
  3374.     mov     DX12,ax
  3375.     mov     cx,ax
  3376.     cwd
  3377.     idiv    bx
  3378.     cmp     cx,0
  3379.     jge     @@tripsl12
  3380.     dec     ax
  3381. @@tripsl12:
  3382.     mov     XA12,ax
  3383.     imul    bx
  3384.     sub     cx,ax
  3385.     mov     DP12,cx
  3386.  
  3387. @@triconstcomputed:
  3388.     mov     ax,DX01
  3389.     imul    word ptr DY02
  3390.     mov     bx,ax
  3391.     mov     cx,dx
  3392.  
  3393.     mov     ax,DX02
  3394.     imul    word ptr DY01
  3395.     cmp     cx,dx
  3396.     jg      @@tript1rt
  3397.     jl      @@tript1lt
  3398.     cmp     bx,ax
  3399.     ja      @@tript1rt
  3400.     jb      @@tript1lt
  3401.     jmp     @@tridone
  3402.  
  3403. @@tript1lt:
  3404.     mov     ax,0
  3405.     mov     bx,0
  3406.     mov     cx,Y0
  3407.     mov     si,X0
  3408.     mov     di,si
  3409.     dec     si
  3410. @@triltloop:
  3411.     inc     cx
  3412.  
  3413.     add     ax,DP02
  3414.     jle     @@triltshortl
  3415.     sub     ax,DY02
  3416.     inc     si
  3417. @@triltshortl:
  3418.     add     si,XA02
  3419.     add     bx,DP01
  3420.     jle     @@triltshortr
  3421.     sub     bx,DY01
  3422.     inc     di
  3423. @@triltshortr:
  3424.     add     di,XA01
  3425.     push    si
  3426.     push    di
  3427.     cmp     cx,Y1
  3428.     jl      @@triltloop
  3429.     jmp     @@trilbstart
  3430. @@trilbloop:
  3431.     inc     cx
  3432.     add     ax,DP02
  3433.     jle     @@trilbshortl
  3434.     sub     ax,DY02
  3435.     inc     si
  3436. @@trilbshortl:
  3437.     add     si,XA02
  3438.  
  3439.     add     bx,DP12
  3440.     jle     @@trilbshortr
  3441.     sub     bx,DY12
  3442.     inc     di
  3443. @@trilbshortr:
  3444.     add     di,XA12
  3445.  
  3446.     push    si
  3447.     push    di
  3448. @@trilbstart:
  3449.     cmp     cx,Y2
  3450.     jl      @@trilbloop
  3451.     jmp     @@tridrawlines
  3452.  
  3453. @@tript1rt:
  3454.     mov     ax,0
  3455.     mov     bx,0
  3456.     mov     cx,Y0
  3457.     mov     si,X0
  3458.     mov     di,si
  3459.     dec     di
  3460. @@trirtloop:
  3461.     inc     cx
  3462.  
  3463.     add     ax,DP02
  3464.     jle     @@trirtshortl
  3465.     sub     ax,DY02
  3466.     inc     si
  3467. @@trirtshortl:
  3468.     add     si,XA02
  3469.  
  3470.     add     bx,DP01
  3471.     jle     @@trirtshortr
  3472.     sub     bx,DY01
  3473.     inc     di
  3474. @@trirtshortr:
  3475.     add     di,XA01
  3476.  
  3477.     push    di
  3478.     push    si
  3479.     cmp     cx,Y1
  3480.     jl      @@trirtloop
  3481.     jmp     @@trirbstart
  3482.  
  3483. @@trirbloop:
  3484.     inc     cx
  3485.     add     ax,DP02
  3486.     jle     @@trirbshortl
  3487.     sub     ax,DY02
  3488.     inc     si
  3489. @@trirbshortl:
  3490.     add     si,XA02
  3491.  
  3492.     add     bx,DP12
  3493.     jle     @@trirbshorts
  3494.     sub     bx,DY12
  3495.     inc     di
  3496. @@trirbshorts:
  3497.     add     di,XA12
  3498.  
  3499.     push    di
  3500.     push    si
  3501. @@trirbstart:
  3502.     cmp     cx,Y2
  3503.     jl      @@trirbloop
  3504.  
  3505. @@tridrawlines:
  3506.     mov     cx,SCREENSEG
  3507.     mov     es,cx
  3508.     mov     dx,SCINDEX
  3509.     mov     al,MAPMASK
  3510.     out     dx,al
  3511.  
  3512. @@lineloop:
  3513.     pop     ax
  3514.     pop     dx
  3515.     cmp     ax,dx
  3516.     jg      @@tridrawnext
  3517.     mov     bx,Color
  3518.     mov     cx,Y2
  3519.     add     dx,2
  3520.     mov     di,PageOffset
  3521.     call    HLineClipR
  3522. @@tridrawnext:
  3523.     dec     word ptr Y2
  3524.     dec     word ptr DY02
  3525.     jnz     @@lineloop
  3526.  
  3527. @@tridone:
  3528.     pop     ds
  3529.     pop     es
  3530.     pop     di
  3531.     ret
  3532.  
  3533. XTriangle ENDP
  3534.  
  3535. XPolygon PROC FAR vertices : DWord, numvertices : Word, Color : Word,\
  3536.                                     PageOffset : Word
  3537. LOCAL x0 : Word, y0 : Word, tricount : Word
  3538.  
  3539.     mov   cx,numvertices
  3540.     cmp   cx,3
  3541.     jl    @@Done
  3542.     sub   cx,3
  3543.     mov   tricount,cx
  3544.     les   di,vertices
  3545.     mov   ax,es:[di]
  3546.     mov   x0,ax
  3547.     mov   ax,es:[di+2]
  3548.     mov   y0,ax
  3549.  
  3550. @@NextTriangle:
  3551.     add   di, 4
  3552.     mov   ax, x0
  3553.     push  ax
  3554.     mov   ax, y0
  3555.     push  ax
  3556.     mov   ax, es:[di+4]
  3557.     push  ax
  3558.     mov   ax, es:[di+6]
  3559.     push  ax
  3560.     mov   ax,es:[di]
  3561.     push  ax
  3562.     mov   ax,es:[di+2]
  3563.     push  ax
  3564.     mov   ax, color
  3565.     push  ax
  3566.     mov   ax, pageoffset
  3567.     push  ax
  3568.     call  xtriangle
  3569.     dec   tricount
  3570.     jns   @@NextTriangle
  3571. @@Done:
  3572.     ret
  3573.  
  3574. XPolygon ENDP
  3575.  
  3576. XPutCursor PROC FAR X : Word, Y : Word, ATopClip : Word, ABottomClip : Word,\
  3577.                                         ScrnOffs : Word
  3578. LOCAL Height : Word, TopRow : Word, NextLineIncr : Word
  3579.  
  3580.     push  si
  3581.     push  di
  3582.     push  ds
  3583.     mov   ax,seg @data
  3584.     mov   ds,ax
  3585.     cld
  3586.     mov   ax,14
  3587.     mov   bx,Y
  3588.     mov   dx,ATopClip
  3589.     sub   dx,bx
  3590.     jle   @@NotTopClip
  3591.     cmp   dx,ax
  3592.     jnl   @@NotVisible
  3593.     mov   cx,dx
  3594.     sub   ax,dx
  3595.     add   bx,dx
  3596.     jmp   @@VertClipDone
  3597.  
  3598. @@NotVisible:
  3599.     pop   ds
  3600.     pop   di
  3601.     pop   si
  3602.     jmp   @@Done
  3603.  
  3604. @@NotTopClip:
  3605.     mov   dx,ABottomClip
  3606.     sub   dx,bx
  3607.     js    @@NotVisible
  3608.     mov   cx,0
  3609.     cmp   dx,ax
  3610.     jg    @@VertClipDone
  3611.     inc   dx
  3612.     mov   ax,dx
  3613.  
  3614. @@VertClipDone:
  3615.     mov   Height,ax
  3616.     mov   TopRow,cx
  3617.     mov   ax,SCREENSEG
  3618.     mov   es,ax
  3619.     mov   ax,bx
  3620.     mov   cx,ScrnLogicalByteWidth
  3621.     mul   cx
  3622.     mov   di,ax
  3623.     sub   cx,3
  3624.     mov   NextLineIncr,cx
  3625.     mov   cx,X
  3626.     mov   bx,cx
  3627.     shr   cx,2
  3628.     add   di,cx
  3629.     and   bx,3
  3630.     add   di,ScrnOffs
  3631.     mov   ax,42
  3632.     mul   bx
  3633.     mov   si,offset MouseMask
  3634.     add   si,ax
  3635.     mov   ax,3
  3636.     mul   TopRow
  3637.     add   si,ax
  3638.     mov   dx,SCINDEX
  3639.     mov   al,MAPMASK
  3640.     out   dx,al
  3641.     inc   dx
  3642.     mov   ah,byte ptr Height
  3643.     mov   bl,MouseColor
  3644.  
  3645. @@RowLoop:
  3646.     mov   cx,3
  3647.  
  3648. @@ColLoop:
  3649.     lodsb
  3650.     out   dx,al
  3651.     mov   es:[di],bl
  3652.     inc   di
  3653.     loop  @@ColLoop
  3654.  
  3655.     add   di,NextLineIncr
  3656.     dec   ah
  3657.     jnz   @@RowLoop
  3658.  
  3659.     pop   ds
  3660.     pop   di
  3661.     pop   si
  3662.  
  3663. @@Done:
  3664.     ret
  3665.  
  3666. ENDP
  3667.  
  3668. GetBG PROC NEAR
  3669.  
  3670.     push  ds
  3671.     cld
  3672.     mov   cx,ScrnLogicalByteWidth
  3673.     mul   cx
  3674.     add   si,ax
  3675.     sub   cx,3
  3676.     shr   bx,2
  3677.     add   si,bx
  3678.     mov   bx,cx
  3679.     mov   di,BGSaveOffs
  3680.     mov   ax,SCREENSEG
  3681.     mov   es,ax
  3682.     mov   ds,ax
  3683.     mov   dx,GCINDEX
  3684.     mov   ax,BITMASK
  3685.     out   dx,ax
  3686.     mov   dx,SCINDEX
  3687.     mov   al,MAPMASK
  3688.     out   dx,al
  3689.     inc   dx
  3690.     mov   al,0fh
  3691.     out   dx,al
  3692.     mov   cx,14
  3693. @@Loop:
  3694.     movsb
  3695.     movsb
  3696.     movsb
  3697.     add si,bx
  3698.     loop @@Loop
  3699.     mov  dx,GCINDEX+1
  3700.     mov  al,0ffh
  3701.     out  dx,al
  3702.     pop  ds
  3703.     ret
  3704.  
  3705. GetBg ENDP
  3706.  
  3707. RestoreBG PROC NEAR
  3708.  
  3709.     push  ds
  3710.     cld
  3711.     mov   cx,ScrnLogicalByteWidth
  3712.     mul   cx
  3713.     add   di,ax
  3714.     sub   cx,3
  3715.     shr   bx,2
  3716.     add   di,bx
  3717.     mov   si,BGSaveOffs
  3718.     mov   ax,SCREENSEG
  3719.     mov   es,ax
  3720.     mov   ds,ax
  3721.     mov   dx,GCINDEX
  3722.     mov   ax,BITMASK
  3723.     out   dx,ax
  3724.     mov   dx,SCINDEX
  3725.     mov   al,MAPMASK
  3726.     out   dx,al
  3727.     inc   dx
  3728.     mov   al,0fh
  3729.     out   dx,al
  3730.     mov   bx,cx
  3731.     mov   cx,14
  3732. @@Loop:
  3733.     movsb
  3734.     movsb
  3735.     movsb
  3736.     add di,bx
  3737.     loop @@Loop
  3738.     mov  dx,GCINDEX+1
  3739.     mov  al,0ffh
  3740.     out  dx,al
  3741.     pop   ds
  3742.     ret
  3743.  
  3744. RestoreBg ENDP
  3745.  
  3746. UpdateCursor PROC FAR
  3747.     cmp VSyncHandlerActive,1
  3748.     je  @@SkipVS
  3749.     mov dx,InputStatus0
  3750. @@WaitNotVsync:
  3751.     in  al,dx
  3752.     test al,08h
  3753.     jnz @@WaitNotVSync
  3754. @@WaitVSync:
  3755.     in  al,dx
  3756.     test al,08h
  3757.     jz  @@WaitVSync
  3758. @@SkipVS:
  3759.     mov di,OldScrnOffs
  3760.     mov ax,OldY
  3761.     mov bx,OldX
  3762.     call restorebg
  3763.     mov si,VisiblePageOffs
  3764.     mov ax,MouseY
  3765.     mov bx,MouseX
  3766.     mov OldScrnOffs,si
  3767.     mov OldY,ax
  3768.     mov OldX,bx
  3769.     call getbg
  3770.     push OldX
  3771.     push OldY
  3772.     mov  ax,0
  3773.     push ax
  3774.     mov  ax,ScrnPhysicalHeight
  3775.     push ax
  3776.     push VisiblePageOffs
  3777.     call xputcursor
  3778.     ret
  3779.  
  3780. UpdateCursor ENDP
  3781.  
  3782. MouseHandler PROC FAR
  3783.  
  3784.     push  ds
  3785.     mov   di,seg @data
  3786.     mov   ds,di
  3787.     cmp   inhandler,1
  3788.     jne   @@NotActive
  3789.     jmp   @@Done2
  3790. @@NotActive:
  3791.     mov   inhandler,1
  3792.     mov   MouseButtonStatus,bx
  3793.     test  ax,1
  3794.     jz    @@Done
  3795.     shr   cx,1
  3796.     mov   MouseX,cx
  3797.     mov   MouseY,dx
  3798.     cmp   MouseHidden,1
  3799.     je    @@Done
  3800.     cmp   MouseFrozen,1
  3801.     je    @@Done
  3802.     cmp   VSyncHandlerActive,1
  3803.     je    @@VSyncHandle
  3804.     call  updatecursor
  3805.     jmp   short @@Done
  3806. @@VSyncHandle:
  3807.     mov   MouseRefreshFlag,1
  3808. @@Done:
  3809.     mov   inhandler,0
  3810. @@Done2:
  3811.     pop  ds
  3812.     ret
  3813.  
  3814. MouseHandler ENDP
  3815.  
  3816. XDefineMouseCursor PROC FAR MouseDef : DWord, MouseColour : Byte
  3817.  
  3818.     cmp   MouseInstalled,0
  3819.     je    @@Done
  3820.     mov   al,MouseColour
  3821.     mov   MouseColor,al
  3822.     push  si
  3823.     push  di
  3824.     push  ds
  3825.     mov   ax,ds
  3826.     mov   es,ax
  3827.     mov   di,offset MouseMask
  3828.     lds   si,MouseDef
  3829.     xor   cl,cl
  3830. @@AlignmentLoop:
  3831.     push  si
  3832.     mov   dh,14
  3833. @@RowLoop:
  3834.     lodsb
  3835.     xor   ah,ah
  3836.     shl   ax,cl
  3837.     mov   bl,al
  3838.     and   bl,0fh
  3839.     mov   es:[di],bl
  3840.     inc   di
  3841.     shr   al,4
  3842.     stosw
  3843.     dec   dh
  3844.     jnz   @@RowLoop
  3845.     pop   si
  3846.     inc   cl
  3847.     cmp   cl,4
  3848.     jne   @@AlignmentLoop
  3849.     pop   ds
  3850.     pop   di
  3851.     pop   si
  3852. @@Done:
  3853.     ret
  3854.  
  3855. XDefineMouseCursor ENDP
  3856.  
  3857.  
  3858. XMouseInit PROC FAR
  3859.  
  3860.     mov   InHandler,0
  3861.     mov   mousehidden,0
  3862.     mov   BGSaveOffs,0
  3863.     mov   MouseX,0
  3864.     mov   MOuseY,0
  3865.     mov   MouseButtonStatus,0
  3866.     mov   MouseFrozen,0
  3867.     mov   MouseColor,0
  3868.     mov   MouseInstalled,0
  3869.     cmp   MouseButtonCount,0
  3870.     jne   @@DontInitialize
  3871.     xor   ax,ax
  3872.     int   33h
  3873.     or    ax,ax
  3874.     jz    @@Done
  3875.     mov   MouseButtonCount,bx
  3876.  
  3877. @@DontInitialize:
  3878.     mov   MouseInstalled,ax
  3879.     or    ax,ax
  3880.     jz    @@Done
  3881.     mov   ax,NonVisualOffs
  3882.     mov   BGSaveOffs,ax
  3883.     add   ax,42
  3884.     mov   NonVisualOffs,ax
  3885.     mov   ax,02
  3886.     int   33h
  3887.     mov   MouseInstalled,1
  3888.     mov   ax,07h
  3889.     mov   cx,0
  3890.     mov   dx,ScrnPhysicalPixelWidth
  3891.     shl   dx,1
  3892.     int   33h
  3893.     mov   ax,08h
  3894.     mov   cx,0
  3895.     mov   dx,ScrnPhysicalHeight
  3896.     int   33h
  3897.     mov   ax,0fh
  3898.     mov   cx,4
  3899.     mov   dx,8
  3900.     int   33h
  3901.     mov   ax,3
  3902.     int   33h
  3903.     mov   MouseY,dx
  3904.     shr   cx,1
  3905.     mov   MouseX,cx
  3906.     mov   ax,12
  3907.     mov   bx,seg mousehandler
  3908.     mov   es,bx
  3909.     mov   dx,offset mousehandler
  3910.     mov   cx,1fh
  3911.     int   33h
  3912.     mov   MouseHidden,1
  3913.     push  ds
  3914.     mov   ax,offset InitMouseDef
  3915.     push  ax
  3916.     mov   al, MouseColor
  3917.     push  ax
  3918.     call  xdefinemousecursor
  3919.     cmp   VSyncHandlerActive,1
  3920.     jne   @@AllIsWell
  3921.     mov   ax, offset updatecursor
  3922.     mov   dx, seg updatecursor
  3923.     cli
  3924.     mov   word ptr [MouseVSyncHandler],ax
  3925.     mov   word ptr [MouseVsyncHandler+2],dx
  3926.     mov   MouseRefreshFlag,0
  3927.     sti
  3928. @@AllIsWell:
  3929.     mov   ax,MouseInstalled
  3930. @@Done:
  3931.     ret
  3932.  
  3933. XMouseInit ENDP
  3934.  
  3935. XMouseWindow PROC FAR x0 : Word, y0 : Word, x1 : Word, y1 : Word
  3936.  
  3937.     mov   ax,7
  3938.     mov   cx,x0
  3939.     shl   cx,1
  3940.     mov   dx,x1
  3941.     shl   dx,1
  3942.     int   33h
  3943.     mov   ax,8
  3944.     mov   cx,y0
  3945.     mov   dx,y1
  3946.     int   33h
  3947.     ret
  3948.  
  3949. XMouseWindow ENDP
  3950.  
  3951.  
  3952. XShowMouse PROC FAR
  3953.  
  3954.     cmp   MouseInstalled,0
  3955.     je    @@Done
  3956.     cmp   MouseHidden,0
  3957.     je    @@Done
  3958.     push  si
  3959.     push  di
  3960.  
  3961. @@WaitEndOfHandler:
  3962.     mov   cl,inhandler
  3963.     or    cl,cl
  3964.     jnz   @@WaitEndOfHandler
  3965.     mov   si,VisiblePageOffs
  3966.     mov   ax,MouseY
  3967.     mov   bx,MouseX
  3968.     mov   OldScrnOffs,si
  3969.     mov   OldY,ax
  3970.     mov   OldX,bx
  3971.     call  getbg
  3972.     push  OldX
  3973.     push  OldY
  3974.     xor   ax,ax
  3975.     push  ax
  3976.     push  ScrnLogicalHeight
  3977.     push  VisiblePageOffs
  3978.     call  xputcursor
  3979.     mov   MouseHidden,0
  3980.     pop   di
  3981.     pop   si
  3982. @@Done:
  3983.     ret
  3984.  
  3985. XShowMouse ENDP
  3986.  
  3987. XHideMouse PROC FAR
  3988.  
  3989.     cmp   MouseInstalled,0
  3990.     je    @@Done
  3991.     cmp   MouseHidden,0
  3992.     jne   @@Done
  3993.     push  si
  3994.     push  di
  3995. @@WaitEndOfHandler:
  3996.     mov   cl,inhandler
  3997.     or    cl,cl
  3998.     jnz   @@WaitEndOfHandler
  3999.     mov   MouseHidden,TRUE
  4000.     mov   di,OldScrnOffs
  4001.     mov   ax,OldY
  4002.     mov   bx,OldX
  4003.     call  restorebg
  4004.     pop   di
  4005.     pop   si
  4006. @@Done:
  4007.     ret
  4008.  
  4009. XHideMouse ENDP
  4010.  
  4011. XMouseRemove PROC FAR
  4012.  
  4013.     cmp   MouseInstalled,0
  4014.     je    @@Done
  4015.     call  xhidemouse
  4016.     mov   ax,12
  4017.     xor   cx,cx
  4018.     int   33h
  4019.     mov   MouseRefreshFlag,0
  4020.     mov   MouseInstalled,0
  4021. @@Done:
  4022.     ret
  4023.  
  4024. XMouseRemove ENDP
  4025.  
  4026. XPositionMouse PROC FAR X : Word, Y : Word
  4027.  
  4028. @@WaitEndOfHandler:
  4029.     mov   bl,inhandler
  4030.     or    bl,bl
  4031.     jnz   @@WaitEndOfHandler
  4032.     mov   ax,4
  4033.     mov   cx,X
  4034.     mov   dx,Y
  4035.     mov   MouseX,cx
  4036.     mov   MouseY,dx
  4037.     shl   cx,1
  4038.     mov   inhandler,1
  4039.     int   33h
  4040.     cmp   MouseHidden,FALSE
  4041.     jne   @@NotVisible
  4042.     push  di
  4043.     push  si
  4044.     call  updatecursor
  4045.     pop   si
  4046.     pop   di
  4047. @@NotVisible:
  4048.     mov   inhandler,0
  4049.     ret
  4050.  
  4051. XPositionMouse ENDP
  4052.  
  4053. XUpdateMouse PROC FAR
  4054.  
  4055.     cmp   MouseInstalled,0
  4056.     je    @@Done
  4057.     cmp   MouseHidden,0
  4058.     jne   @@Done
  4059.     mov   ax,03h
  4060.     int   33h
  4061.     shr   cx,1
  4062.     mov   MouseX,cx
  4063.     mov   MouseY,dx
  4064.     mov   MouseButtonStatus,bx
  4065.     cmp   VSyncHandlerActive,1
  4066.     je    @@HandleVSync
  4067.     call  updatecursor
  4068.     jmp   short @@Done
  4069. @@HandleVSync:
  4070.     mov   MouseRefreshFlag,1
  4071. @@Done:
  4072.     ret
  4073.  
  4074. XUpdateMouse ENDP
  4075.  
  4076.  
  4077. XFloodFill PROC FAR X : Word, Y : Word, PgOfs : Word, Color : Word
  4078. LOCAL len : Word, y1 : Word, y2 : Word, deltax : Word, floodval : Word,\
  4079.             Astackptr : Word, FillCount : Word
  4080.  
  4081.     mov  FillCount,0
  4082.     mov  si,Y
  4083.     mov  ax,ScrnLogicalByteWidth
  4084.     mul  si
  4085.     mov  di,X
  4086.     mov  bx,di
  4087.     shr  di,2
  4088.     add  di,ax
  4089.     add  di,PgOfs
  4090.     mov  ax,SCREENSEG
  4091.     mov  es,ax
  4092.     mov  ah,bl
  4093.     and  ah,011b
  4094.     mov  al,READMAP
  4095.     mov  dx,GCINDEX
  4096.     out  dx,ax
  4097.  
  4098.     mov  al,es:[di]
  4099.     cmp  al,byte ptr Color
  4100.     je   @@Done
  4101.  
  4102.     mov  cx,LeftClip
  4103.     sal  cx,2
  4104.     cmp  bx,cx
  4105.     jl   @@Done
  4106.  
  4107.     mov  cx,RightClip
  4108.     sal  cx,2
  4109.     cmp  bx,cx
  4110.     jg   @@Done
  4111.  
  4112.     mov  floodval,ax
  4113.  
  4114.     push bx
  4115.     push si
  4116.     push si
  4117.     mov  cx,1
  4118.     push cx
  4119.     mov  Astackptr,1
  4120.  
  4121.     mov  deltax,-1
  4122.     mov  y1,si
  4123.     mov  y2,si
  4124.     jmp  @@entry
  4125.  
  4126. @@Done2:
  4127.     jmp @@Done
  4128.  
  4129. @@NextScanCol:
  4130.     dec  Astackptr
  4131.     js   @@Done2
  4132.  
  4133. @@WhileLoop:
  4134.     pop  cx
  4135.     mov  deltax,cx
  4136.     pop  ax
  4137.     mov  y2,ax
  4138.     pop  si
  4139.     mov  y1,si
  4140.     pop  bx
  4141.  
  4142.     sub  ax,si
  4143.     jns  @@PositiveY
  4144.     neg  ax
  4145. @@PositiveY:
  4146.     add  FillCount,ax
  4147.     add  bx,cx
  4148.     mov  ax,bx
  4149.     sar  ax,2
  4150.     cmp  ax,LeftClip
  4151.     jl   @@NextScanCol
  4152.     cmp  ax,RightClip
  4153.     jg   @@NextScanCol
  4154.  
  4155.     mov  ah,bl
  4156.     and  ah,011b
  4157.     mov  al,READMAP
  4158.     mov  dx,GCINDEX
  4159.     out  dx,ax
  4160.  
  4161. @@entry:
  4162.     mov  cl,bl
  4163.     and  cl,011b
  4164.     mov  ax,0100h + MAPMASK
  4165.     shl  ah,cl
  4166.     mov  dx,SCINDEX
  4167.     out  dx,ax
  4168.     mov  ax,ScrnLogicalByteWidth
  4169.     mov  cx,ax
  4170.     mul  si
  4171.     mov  di,bx
  4172.     shr  di,2
  4173.     add  di,ax
  4174.     add  di,PgOfs
  4175.     mov  dx,di
  4176.  
  4177.     mov  al,byte ptr Color
  4178.     mov  ah,byte ptr floodval
  4179.  
  4180. @@FillColUpward:
  4181.     cmp  si,TopClip
  4182.     jl   @@UpwardFillDone
  4183.  
  4184.     cmp  es:[di],ah
  4185.     jne  @@UpwardFillDone
  4186.  
  4187.     mov  es:[di],al
  4188.     sub  di,cx
  4189.     dec  si
  4190.     jmp  @@FillColUpward
  4191.  
  4192. @@UpwardFillDone:
  4193.     cmp  si,y1
  4194.     jge  @@Skip
  4195.  
  4196.     inc  si
  4197.     mov  len,si
  4198.  
  4199.     cmp  si,y1
  4200.     jge  @@AtColumnTop
  4201.  
  4202.     push bx
  4203.     push si
  4204.     mov  ax,y1
  4205.     dec  ax
  4206.     push ax
  4207.     mov  ax,deltax
  4208.     neg  ax
  4209.     push ax
  4210.     inc  Astackptr
  4211.  
  4212. @@AtColumnTop:
  4213.     mov  si,y1
  4214.     mov  di,dx
  4215.     add  di,cx
  4216.     inc  si
  4217.  
  4218.  
  4219. @@ColumnLoop:
  4220.     mov  ah,byte ptr floodval
  4221.     mov  al,byte ptr Color
  4222.  
  4223. @@DownwardFill:
  4224.     cmp  si,BottomClip
  4225.     jg   @@DownwardFillDone
  4226.     cmp  es:[di],ah
  4227.     jne  @@DownwardFillDone
  4228.     mov  es:[di],al
  4229.     add  di,cx
  4230.     inc  si
  4231.     jmp  @@DownwardFill
  4232.  
  4233. @@DownwardFillDone:
  4234.  
  4235.     push bx
  4236.     mov  ax,len
  4237.     push ax
  4238.     mov  ax,si
  4239.     dec  ax
  4240.     push ax
  4241.     mov  ax,deltax
  4242.     push ax
  4243.     inc  Astackptr
  4244.  
  4245.     mov  ax,y2
  4246.     inc  ax
  4247.     cmp  si,ax
  4248.     jle  @@Skip
  4249.  
  4250.     push bx
  4251.     push ax
  4252.     mov  ax,si
  4253.     dec  ax
  4254.     push ax
  4255.     mov  ax,deltax
  4256.     neg  ax
  4257.     push ax
  4258.     inc  Astackptr
  4259.  
  4260. @@Skip:
  4261.     mov  ah,byte ptr floodval
  4262.     mov  dx,y2
  4263.  
  4264. @@Backtrack:
  4265.     add  di,cx
  4266.     inc  si
  4267.     cmp  si,dx
  4268.     jg   @@BacktrackDone
  4269.  
  4270.     cmp  byte ptr es:[di],ah
  4271.     jne  @@Backtrack
  4272.  
  4273. @@BacktrackDone:
  4274.     mov  len,si
  4275.     cmp  si,dx
  4276.     jle  @@ColumnLoop
  4277.  
  4278.     dec  Astackptr
  4279.     js   @@Done
  4280.     jmp  @@WhileLoop
  4281. @@Done:
  4282.     mov  ax,FillCount
  4283.     ret
  4284.  
  4285. XFloodFill ENDP
  4286.  
  4287.  
  4288. XBoundaryFill PROC FAR X : Word, Y : Word, PgOfs : Word, BoundaryColor : Word,\
  4289.                                              Color : Word
  4290. LOCAL len : Word, y1 : Word, y2 : Word, deltax : Word, y1offs : Word,\
  4291.             Astackptr : Word, FillCount : Word
  4292.  
  4293.     mov  FillCount,0
  4294.     mov  si,Y
  4295.     mov  ax,ScrnLogicalByteWidth
  4296.     mul  si
  4297.     mov  di,X
  4298.     mov  bx,di
  4299.     shr  di,2
  4300.     add  di,ax
  4301.     add  di,PgOfs
  4302.     mov  ax,SCREENSEG
  4303.     mov  es,ax
  4304.     mov  ah,bl
  4305.     and  ah,011b
  4306.     mov  al,READMAP
  4307.     mov  dx,GCINDEX
  4308.     out  dx,ax
  4309.     mov  al,es:[di]
  4310.     cmp  al,byte ptr Color
  4311.     je   @@Done
  4312.     cmp  al,byte ptr BoundaryColor
  4313.     je   @@Done
  4314.  
  4315.  
  4316.     mov  cx,LeftClip
  4317.     sal  cx,2
  4318.     cmp  bx,cx
  4319.     jl   @@Done
  4320.  
  4321.     mov  cx,RightClip
  4322.     sal  cx,2
  4323.     cmp  bx,cx
  4324.     jg   @@Done
  4325.  
  4326.     push bx
  4327.     push si
  4328.     push si
  4329.     mov  cx,1
  4330.     push cx
  4331.     mov  Astackptr,1
  4332.     mov  al,byte ptr BoundaryColor
  4333.     mov  byte ptr [Color+1],al
  4334.  
  4335.     mov  deltax,-1
  4336.     mov  y1,si
  4337.     mov  y2,si
  4338.     jmp  @@entry
  4339.  
  4340. @@NextScanCol:
  4341.     dec  Astackptr
  4342.     js   @@Done
  4343.  
  4344. @@WhileLoop:
  4345.     pop  cx
  4346.     mov  deltax,cx
  4347.     pop  ax
  4348.     mov  y2,ax
  4349.     pop  si
  4350.     mov  y1,si
  4351.     pop  bx
  4352.     add  bx,cx
  4353.  
  4354.     sub  ax,si
  4355.     jns  @@PositiveY
  4356.     neg  ax
  4357. @@PositiveY:
  4358.     add  FillCount,ax
  4359.     mov  ax,bx
  4360.     sar  ax,2
  4361.     cmp  ax,LeftClip
  4362.     jl   @@NextScanCol
  4363.     cmp  ax,RightClip
  4364.     jg   @@NextScanCol
  4365.     mov  ah,bl
  4366.     and  ah,011b
  4367.     mov  al,READMAP
  4368.     mov  dx,GCINDEX
  4369.     out  dx,ax
  4370.  
  4371. @@entry:
  4372.     mov  cl,bl
  4373.     and  cl,011b
  4374.     mov  ax,0100h + MAPMASK
  4375.     shl  ah,cl
  4376.     mov  dx,SCINDEX
  4377.     out  dx,ax
  4378.     mov  ax,ScrnLogicalByteWidth
  4379.     mov  cx,ax
  4380.     mul  si
  4381.     mov  di,bx
  4382.     shr  di,2
  4383.     add  di,ax
  4384.     add  di,PgOfs
  4385.     mov  y1offs,di
  4386.     mov   ax,Color
  4387.  
  4388. @@FillColUpward:
  4389.     cmp  si,TopClip
  4390.     jl   @@UpwardFillDone
  4391.  
  4392.     mov  dl,es:[di]
  4393.     cmp  dl,ah
  4394.     je   @@UpwardFillDone
  4395.  
  4396.     cmp  dl,al
  4397.     je   @@UpwardFillDone
  4398.  
  4399.     mov  es:[di],al
  4400.     sub  di,cx
  4401.     dec  si
  4402.     jmp  @@FillColUpward
  4403.  
  4404. @@UpwardFillDone:
  4405.     cmp  si,y1
  4406.     jge  @@Skip
  4407.  
  4408.     inc  si
  4409.     mov  len,si
  4410.  
  4411.     cmp  si,y1
  4412.     jge  @@AtColumnTop
  4413.  
  4414.     push bx
  4415.     push si
  4416.     mov  ax,y1
  4417.     dec  ax
  4418.     push ax
  4419.     mov  ax,deltax
  4420.     neg  ax
  4421.     push ax
  4422.     inc  Astackptr
  4423.  
  4424. @@AtColumnTop:
  4425.     mov  si,y1
  4426.     mov  di,y1offs
  4427.     add  di,cx
  4428.     inc  si
  4429.  
  4430.  
  4431. @@ColumnLoop:
  4432.     mov   ax,Color
  4433.  
  4434. @@DownwardFill:
  4435.     cmp  si,BottomClip
  4436.     jg   @@DownwardFillDone
  4437.  
  4438.     cmp  es:[di],ah
  4439.     je   @@DownwardFillDone
  4440.  
  4441.     cmp  es:[di],al
  4442.     je   @@DownwardFillDone
  4443.  
  4444.     mov  es:[di],al
  4445.     add  di,cx
  4446.     inc  si
  4447.     jmp  @@DownwardFill
  4448.  
  4449. @@DownwardFillDone:
  4450.  
  4451.     push bx
  4452.     mov  ax,len
  4453.     push ax
  4454.     mov  ax,si
  4455.     dec  ax
  4456.     push ax
  4457.     mov  ax,deltax
  4458.     push ax
  4459.     inc  Astackptr
  4460.  
  4461.     mov  ax,y2
  4462.     inc  ax
  4463.     cmp  si,ax
  4464.     jle  @@Skip
  4465.  
  4466.     push bx
  4467.     push ax
  4468.     mov  ax,si
  4469.     dec  ax
  4470.     push ax
  4471.     mov  ax,deltax
  4472.     neg  ax
  4473.     push ax
  4474.     inc  Astackptr
  4475.  
  4476. @@Skip:
  4477.     mov  ax,Color
  4478.  
  4479. @@Backtrack:
  4480.     add  di,cx
  4481.     inc  si
  4482.     cmp  si,y2
  4483.     jg   @@BacktrackDone
  4484.  
  4485.     mov  dl,byte ptr es:[di]
  4486.     cmp  dl,al
  4487.     je   @@Backtrack
  4488.  
  4489.     cmp  dl,ah
  4490.     je   @@Backtrack
  4491.  
  4492. @@BacktrackDone:
  4493.     mov  len,si
  4494.     cmp  si,y2
  4495.     jle  @@ColumnLoop
  4496.  
  4497.     dec  Astackptr
  4498.     js   @@Done
  4499.     jmp  @@WhileLoop
  4500. @@Done:
  4501.     mov  ax,FillCount
  4502.     ret
  4503.  
  4504. XBoundaryFill ENDP
  4505.  
  4506. getvsyncperiod proc near
  4507.     mov    al,TIMERMODE            ;Start timer
  4508.     out    TIMERCONTROL,al
  4509.     mov    al,0
  4510.     out    TIMER0,al
  4511.     out    TIMER0,al
  4512.  
  4513.     WaitVsyncStart
  4514.  
  4515.     mov    al,LATCHCOUNT
  4516.     out    TIMERCONTROL,al
  4517.     in     al,TIMER0
  4518.     mov    cl,al
  4519.     in     al,TIMER0
  4520.     mov    ch,al                    ;cx=65536-clicks
  4521.  
  4522.     WaitVsyncStart
  4523.  
  4524.     mov    al,LATCHCOUNT
  4525.     out    TIMERCONTROL,al
  4526.     in     al,TIMER0
  4527.     mov    dl,al
  4528.     in     al,TIMER0
  4529.     mov    dh,al
  4530.  
  4531.     sub    cx,dx
  4532.     mov    ax,cx
  4533.     ret
  4534. getvsyncperiod endp
  4535.  
  4536. vsyncint proc far
  4537.  
  4538.     pusha
  4539.     push   ds
  4540.     push   es
  4541.  
  4542.     mov    ax,@data
  4543.     mov    ds,ax
  4544.     add    VsyncIntTicksLo,1
  4545.     adc    VsyncIntTicksHi,0
  4546.  
  4547.     inc    ElapsedVrts
  4548.     mov    cx,ElapsedVrts
  4549.     cmp    cx,VrtsToSkip
  4550.     jl     @@StopClock
  4551.  
  4552.     cmp    StartAddressFlag,1
  4553.     jne    @@StopClock
  4554.  
  4555.     mov    dx,CRTCINDEX
  4556.     mov    ax,WaitingStartLow
  4557.     mov    bx,WaitingStartHigh
  4558.     out    dx,ax
  4559.     mov    ax,bx
  4560.     out    dx,ax
  4561.  
  4562. @@StopClock:
  4563.     cli
  4564.     mov    al,TIMERMODE
  4565.     out    TIMERCONTROL,al
  4566.     mov    al,255
  4567.     out    TIMER0,al
  4568.     out    TIMER0,al
  4569.     sti
  4570.  
  4571.     cli
  4572.     mov    dx,INPUTSTATUS0
  4573. @@WaitVS:
  4574.     in     al,dx
  4575.     test   al,08h
  4576.     jz     @@WaitVS
  4577.  
  4578.     mov    al,TIMERMODE
  4579.     out    TIMERCONTROL,al
  4580.     mov    ax,ClockRate
  4581.     out    TIMER0,al
  4582.     mov    al,ah
  4583.     out    TIMER0,al
  4584.  
  4585.     cmp    cx,VrtsToSkip
  4586.     jl     @@PaletteInt
  4587.  
  4588.     cmp    StartAddressFlag,1
  4589.     jne    @@PaletteInt
  4590.  
  4591.     xor    cx,cx
  4592.     mov    ElapsedVrts,cx
  4593.  
  4594.     mov    ax,WaitingPelPan
  4595.     mov    dx,ACINDEX
  4596.     out    dx,al
  4597.     mov    al,ah
  4598.     out    dx,al
  4599.  
  4600. @@PaletteInt:
  4601.     mov    StartAddressFlag,0
  4602.     cmp    VsyncPaletteCount,0
  4603.     je     @@MouseInt
  4604.     mov    si, offset VsyncPaletteBuffer
  4605.     mov    cx, VsyncPaletteCount
  4606.     mov    ax, cx
  4607.     shl    cx, 1
  4608.     add    cx, ax
  4609.     mov    ax, VsyncPaletteStart
  4610.     mov    dx, DACWRITEINDEX
  4611.     out    dx, al
  4612.     mov    dx, DACDATA
  4613.  
  4614.     rep    outsb
  4615.     mov    VsyncPaletteCount,0
  4616.  
  4617. @@MouseInt:
  4618.     cmp    MouseRefreshFlag,1
  4619.     jne    @@UserInt
  4620.     call   dword ptr [MouseVsyncHandler]
  4621.     mov    MouseRefreshFlag,0
  4622.  
  4623. @@UserInt:
  4624.     cmp    UserVsyncSeg, 0
  4625.     je     short @@Sim182
  4626.     cmp    InUserHandler,0
  4627.     jne    short @@Sim182
  4628.     mov    InUserHandler,1
  4629.     mov    StackSeg,ss
  4630.     mov    StackPtr,sp
  4631.     push   ds
  4632.     pop    ss
  4633.     mov    sp, offset LocalStack
  4634.     sti
  4635.     call   dword ptr [UserVsyncHandler]
  4636.     cli
  4637.     mov    sp, StackPtr
  4638.     mov    ss, StackSeg
  4639.     mov    InUserHandler,0
  4640.  
  4641. @@Sim182:
  4642.     mov    ax,[VsyncPeriod]
  4643.     add    [ClockCounter],ax
  4644.     jnc    short @@DontChainOld
  4645.     pop    es
  4646.     pop    ds
  4647.     popa
  4648.     sti
  4649.     pushf
  4650.     db     09ah            ; call far
  4651. OldTimerInt dd 0         ; Self modifying code
  4652.     iret
  4653.  
  4654. @@DontChainOld:
  4655.     mov    al,NONSPECEOI
  4656.     out    PICCMD,al
  4657.     pop    es
  4658.     pop    ds
  4659.     popa
  4660.     sti
  4661.     iret
  4662.  
  4663. vsyncint endp
  4664.  
  4665.  
  4666. xinstallvsynchandler proc far VrtSkipCount:word
  4667.  
  4668.     mov     ax,[VrtSkipCount]
  4669.     or      ax,ax
  4670.     jnz     @@NonZeroCount
  4671.     mov     ax,1
  4672. @@NonZeroCount:
  4673.     mov     [VrtsToSkip],ax
  4674.     mov     [ElapsedVrts],0
  4675.     cmp     [VsyncHandlerActive],TRUE      ;Is it already active
  4676.     je      short @@Return
  4677.     call    getvsyncperiod                ;no, get the vsync period
  4678.  
  4679.     mov     [VsyncPeriod],ax
  4680.     sub     ax,INTINADVANCE               ;We need a little extra
  4681.     mov     [ClockRate],ax                  ;time
  4682.  
  4683.     mov     dx,18                           ;dx:ax=1193000
  4684.     mov     ax,13352
  4685.     idiv    [VsyncPeriod]
  4686.     mov     [TicksPerSecond],ax            ;1193/VsyncPeriod
  4687.  
  4688.     mov     word ptr [VsyncIntTicks],0
  4689.     mov     word ptr [VsyncIntTicks+2],0
  4690.     mov     MouseRefreshFlag, 0
  4691.     mov     StartAddressFlag, 0
  4692.     mov     VSyncPaletteCount, 0
  4693.  
  4694.     cli
  4695.     
  4696. IFDEF _DPMI_
  4697.     mov     ax, dpmiGetInt
  4698.     mov     bl, TIMERVECT
  4699.     int     DPMI                          ;Get Pmode vector
  4700.     mov     ax, cs                        ;vector = cx:dx
  4701.     add     ax, SelectorInc               ;Make Es = Cs so we can write to it
  4702.     mov     es, ax
  4703.     mov     word ptr es:[OldTimerInt],dx
  4704.     mov     word ptr es:[OldTimerInt+2],cx
  4705. ELSE
  4706.     mov     ax, DOSGETVECT+TIMERVECT      ;Get address of old timer int
  4707.     int     21h                           ;vector = es:bx
  4708.     mov     ax,es
  4709.     mov     word ptr cs:[OldTimerInt],bx       ;Store in OldTimerInt
  4710.     mov     word ptr cs:[OldTimerInt+2],ax
  4711. ENDIF
  4712.     mov     [VsyncHandlerActive],TRUE      ;Mark handler as active
  4713. IFDEF _DPMI_
  4714.     mov     ax, dpmiSetInt
  4715.     mov     bl, TIMERVECT
  4716.     mov     dx, offset vsyncint
  4717.     mov     cx, cs
  4718.     int     DPMI
  4719. ELSE
  4720.     mov     ax,DOSSETVECT+TIMERVECT       ;Set the new timer int
  4721.     push    ds
  4722.     mov     dx,cs
  4723.     mov     ds,dx
  4724.     mov     dx,offset vsyncint
  4725.     int     21h
  4726.     pop     ds
  4727. ENDIF
  4728.     mov     al,TIMERMODE                   ;Reprogram timer 0
  4729.     out     TIMERCONTROL,al
  4730.     mov     ax,ClockRate
  4731.     out     TIMER0,al
  4732.     mov     al,ah
  4733.     out     TIMER0,al
  4734.     sti
  4735. @@Return:
  4736.     ret
  4737.  
  4738. xinstallvsynchandler endp
  4739.  
  4740. xremovevsynchandler proc far
  4741.  
  4742.     cmp     [VsyncHandlerActive],FALSE
  4743.     je      short @@Return
  4744.     mov     dx, word ptr cs:[OldTimerInt]
  4745.     mov     cx, word ptr cs:[OldTimerInt+2]
  4746. IFDEF _DPMI_
  4747.     mov     ax, dpmiSetInt
  4748.     mov     bl, TIMERVECT
  4749.     int     DPMI
  4750. ELSE
  4751.     push    ds
  4752.     mov     ds,cx
  4753.     mov     ax,DOSSETVECT+TIMERVECT       ;Restore the old timer int
  4754.     cli
  4755.     int     21h
  4756.     pop     ds
  4757. ENDIF
  4758.     mov     al,TIMERMODE                   ;Restore timer 0
  4759.     out     TIMERCONTROL,al
  4760.     mov     al,0
  4761.     out     TIMER0,al
  4762.     out     TIMER0,al
  4763.     sti
  4764. @@Return:
  4765.     ret
  4766.  
  4767. xremovevsynchandler endp
  4768.  
  4769.  
  4770. xsetuservsynchandler proc far handlerproc:dword
  4771.     mov     ax, word ptr [handlerproc]
  4772.     mov     dx, word ptr [handlerproc+2]
  4773.     cli
  4774.     mov     word ptr [UserVsyncHandler],ax
  4775.     mov     word ptr [UserVsyncHandler+2],dx
  4776.     sti
  4777.     ret
  4778. xsetuservsynchandler endp
  4779.  
  4780.  
  4781. END
  4782.